mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-21 08:02:39 +08:00
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>
This commit is contained in:
@@ -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;
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
@@ -100,7 +100,7 @@ export class MouseJoint2D extends Joint2D {
|
||||
}
|
||||
}
|
||||
|
||||
update (dt): void {
|
||||
update (dt: number): void {
|
||||
this._joint!.update!(dt);
|
||||
}
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user