From 48308cdbefd2ca27bc566bd4ca4158b0e95f6cc7 Mon Sep 17 00:00:00 2001 From: James Chen Date: Tue, 11 Mar 2025 18:44:36 +0800 Subject: [PATCH] Fix touch listeners are not unsubscribed in MouseJoint. (#18438) * MouseJoint Touch Issue This code ensures that all touch event listeners (TOUCH_START, TOUCH_MOVE, TOUCH_END, and TOUCH_CANCEL) attached to the Canvas node are properly removed when the MouseJoint component is destroyed. Purpose: Prevents touch event listeners from remaining in memory after the MouseJoint is destroyed. Avoids unexpected behavior caused by lingering event handlers that could still trigger interactions. Improves memory management and performance by ensuring proper cleanup of event listeners. * Override destroy an remove touch listner from joint * Fix touch listeners are not unsubscribed in MouseJoint. * Unsubscribe touch listeners in onDestroy * Subscribe touch listeners in onEnable/onDisable. * Optimize code size. * Remove empty onDestroy. * Should empty implement MouseJoint. --------- Co-authored-by: gt3mb <68908903+gt3mb@users.noreply.github.com> --- .../box2d-jsb/joints/mouse-joint.ts | 32 ++++++++------- .../box2d-wasm/joints/mouse-joint.ts | 34 +++++++++------- cocos/physics-2d/box2d/joints/mouse-joint.ts | 39 ++++++++++++------- .../framework/components/joints/joint-2d.ts | 10 ++--- .../components/joints/mouse-joint-2d.ts | 2 +- cocos/physics-2d/spec/i-physics-joint.ts | 4 ++ 6 files changed, 72 insertions(+), 49 deletions(-) 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 {