diff --git a/cocos/physics-2d/box2d-jsb/joints/mouse-joint.ts b/cocos/physics-2d/box2d-jsb/joints/mouse-joint.ts index ca80e44cde..0c7b2fc32c 100644 --- a/cocos/physics-2d/box2d-jsb/joints/mouse-joint.ts +++ b/cocos/physics-2d/box2d-jsb/joints/mouse-joint.ts @@ -61,7 +61,7 @@ export class b2MouseJoint extends b2Joint implements IMouseJoint { } } - _createJointDef (): any { + override _createJointDef (): any { const def = new b2jsb.MouseJointDef(); const comp = this._jointComp as MouseJoint2D; // eslint-disable-next-line @typescript-eslint/ban-ts-comment @@ -73,24 +73,30 @@ export class b2MouseJoint extends b2Joint implements IMouseJoint { return def; } - initialize (comp: Joint2D): void { - super.initialize(comp); + override start (): void { + // empty implementation to override its parent's start, so don't remove it. + } + override onEnable (): void { + this._enableTouch(true); + } + + override onDisable (): void { + super.onDisable(); + this._enableTouch(false); + } + + private _enableTouch (v: boolean): void { const canvas = find('Canvas'); if (canvas) { - canvas.on(NodeEventType.TOUCH_START, this.onTouchBegan, this); - canvas.on(NodeEventType.TOUCH_MOVE, this.onTouchMove, this); - canvas.on(NodeEventType.TOUCH_END, this.onTouchEnd, this); - canvas.on(NodeEventType.TOUCH_CANCEL, this.onTouchEnd, this); + const cb = v ? canvas.on : canvas.off; + cb.call(canvas, NodeEventType.TOUCH_START, this.onTouchBegan, this); + cb.call(canvas, NodeEventType.TOUCH_MOVE, this.onTouchMove, this); + cb.call(canvas, NodeEventType.TOUCH_END, this.onTouchEnd, this); + cb.call(canvas, NodeEventType.TOUCH_CANCEL, this.onTouchEnd, this); } } - onEnable (): void { - } - - start (): void { - } - onTouchBegan (event: Touch): void { this._isTouched = true; diff --git a/cocos/physics-2d/box2d-wasm/joints/mouse-joint.ts b/cocos/physics-2d/box2d-wasm/joints/mouse-joint.ts index 14bb68e7fd..0a9ef8d1cc 100644 --- a/cocos/physics-2d/box2d-wasm/joints/mouse-joint.ts +++ b/cocos/physics-2d/box2d-wasm/joints/mouse-joint.ts @@ -63,7 +63,7 @@ export class B2MouseJoint extends B2Joint implements IMouseJoint { } } - _createJointDef (): any { + override _createJointDef (): any { const def = new B2.MouseJointDef(); const comp = this._jointComp as MouseJoint2D; def.target = { x: this._touchPoint.x / PHYSICS_2D_PTM_RATIO, y: this._touchPoint.y / PHYSICS_2D_PTM_RATIO }; @@ -73,26 +73,30 @@ export class B2MouseJoint extends B2Joint implements IMouseJoint { return def; } - initialize (comp: Joint2D): void { - super.initialize(comp); + override start (): void { + // empty implementation to override its parent's start, so don't remove it. + } + override onEnable (): void { + this._enableTouch(true); + } + + override onDisable (): void { + super.onDisable(); + this._enableTouch(false); + } + + private _enableTouch (v: boolean): void { const canvas = find('Canvas'); if (canvas) { - canvas.on(NodeEventType.TOUCH_START, this.onTouchBegan, this); - canvas.on(NodeEventType.TOUCH_MOVE, this.onTouchMove, this); - canvas.on(NodeEventType.TOUCH_END, this.onTouchEnd, this); - canvas.on(NodeEventType.TOUCH_CANCEL, this.onTouchEnd, this); + const cb = v ? canvas.on : canvas.off; + cb.call(canvas, NodeEventType.TOUCH_START, this.onTouchBegan, this); + cb.call(canvas, NodeEventType.TOUCH_MOVE, this.onTouchMove, this); + cb.call(canvas, NodeEventType.TOUCH_END, this.onTouchEnd, this); + cb.call(canvas, NodeEventType.TOUCH_CANCEL, this.onTouchEnd, this); } } - onEnable (): void { - //empty - } - - start (): void { - //empty - } - onTouchBegan (event: Touch): void { this._isTouched = true; diff --git a/cocos/physics-2d/box2d/joints/mouse-joint.ts b/cocos/physics-2d/box2d/joints/mouse-joint.ts index e526ed476d..2ec021927b 100644 --- a/cocos/physics-2d/box2d/joints/mouse-joint.ts +++ b/cocos/physics-2d/box2d/joints/mouse-joint.ts @@ -63,40 +63,49 @@ export class b2MouseJoint extends b2Joint implements IMouseJoint { } } - _createJointDef (): any { + override _createJointDef (): any { const def = new b2.MouseJointDef(); const comp = this._jointComp as MouseJoint2D; - def.target.Set(this._touchPoint.x / PHYSICS_2D_PTM_RATIO, this._touchPoint.y / PHYSICS_2D_PTM_RATIO); + def.target.Set( + this._touchPoint.x / PHYSICS_2D_PTM_RATIO, + this._touchPoint.y / PHYSICS_2D_PTM_RATIO, + ); def.maxForce = comp.maxForce; def.dampingRatio = comp.dampingRatio; def.frequencyHz = comp.frequency; return def; } - initialize (comp: Joint2D): void { - super.initialize(comp); + override start (): void { + // empty implementation to override its parent's start, so don't remove it. + } + override onEnable (): void { + this._enableTouch(true); + } + + override onDisable (): void { + super.onDisable(); + this._enableTouch(false); + } + + private _enableTouch (v: boolean): void { const canvas = find('Canvas'); if (canvas) { - canvas.on(NodeEventType.TOUCH_START, this.onTouchBegan, this); - canvas.on(NodeEventType.TOUCH_MOVE, this.onTouchMove, this); - canvas.on(NodeEventType.TOUCH_END, this.onTouchEnd, this); - canvas.on(NodeEventType.TOUCH_CANCEL, this.onTouchEnd, this); + const cb = v ? canvas.on : canvas.off; + cb.call(canvas, NodeEventType.TOUCH_START, this.onTouchBegan, this); + cb.call(canvas, NodeEventType.TOUCH_MOVE, this.onTouchMove, this); + cb.call(canvas, NodeEventType.TOUCH_END, this.onTouchEnd, this); + cb.call(canvas, NodeEventType.TOUCH_CANCEL, this.onTouchEnd, this); } } - onEnable (): void { - } - - start (): void { - } - onTouchBegan (event: Touch): void { this._isTouched = true; const target = this._touchPoint.set(event.getUILocation()); - const world = (PhysicsSystem2D.instance.physicsWorld as b2PhysicsWorld); + const world = PhysicsSystem2D.instance.physicsWorld as b2PhysicsWorld; const colliders = world.testPoint(target); if (colliders.length <= 0) return; diff --git a/cocos/physics-2d/framework/components/joints/joint-2d.ts b/cocos/physics-2d/framework/components/joints/joint-2d.ts index 8834dd732a..f4a7211874 100644 --- a/cocos/physics-2d/framework/components/joints/joint-2d.ts +++ b/cocos/physics-2d/framework/components/joints/joint-2d.ts @@ -100,7 +100,7 @@ export class Joint2D extends Component { */ TYPE = EJoint2DType.None; - protected onLoad (): void { + protected override onLoad (): void { if (!EDITOR_NOT_IN_PREVIEW) { this._joint = createJoint(this.TYPE); this._joint.initialize(this); @@ -109,25 +109,25 @@ export class Joint2D extends Component { } } - protected onEnable (): void { + protected override onEnable (): void { if (this._joint && this._joint.onEnable) { this._joint.onEnable(); } } - protected onDisable (): void { + protected override onDisable (): void { if (this._joint && this._joint.onDisable) { this._joint.onDisable(); } } - protected start (): void { + protected override start (): void { if (this._joint && this._joint.start) { this._joint.start(); } } - protected onDestroy (): void { + protected override onDestroy (): void { if (this._joint && this._joint.onDestroy) { this._joint.onDestroy(); } diff --git a/cocos/physics-2d/framework/components/joints/mouse-joint-2d.ts b/cocos/physics-2d/framework/components/joints/mouse-joint-2d.ts index 036eb3bc74..43215ce289 100644 --- a/cocos/physics-2d/framework/components/joints/mouse-joint-2d.ts +++ b/cocos/physics-2d/framework/components/joints/mouse-joint-2d.ts @@ -100,7 +100,7 @@ export class MouseJoint2D extends Joint2D { } } - update (dt): void { + update (dt: number): void { this._joint!.update!(dt); } diff --git a/cocos/physics-2d/spec/i-physics-joint.ts b/cocos/physics-2d/spec/i-physics-joint.ts index ae23b55842..52954de42b 100644 --- a/cocos/physics-2d/spec/i-physics-joint.ts +++ b/cocos/physics-2d/spec/i-physics-joint.ts @@ -30,6 +30,10 @@ export interface IJoint2D extends ILifecycle { readonly impl: any; apply (): void; initialize (v: Joint2D): void; + onEnable? (): void; + onDisable? (): void; + start? (): void; + onDestroy? (): void; } export interface IDistanceJoint extends IJoint2D {