diff --git a/cocos/physics-2d/box2d-jsb/physics-contact.ts b/cocos/physics-2d/box2d-jsb/physics-contact.ts index f7ffa529c8..288ba1a308 100644 --- a/cocos/physics-2d/box2d-jsb/physics-contact.ts +++ b/cocos/physics-2d/box2d-jsb/physics-contact.ts @@ -218,39 +218,21 @@ export class PhysicsContact implements IPhysics2DContact { } emit (contactType: string): void { - let func = ''; - switch (contactType) { - case Contact2DType.BEGIN_CONTACT: - func = 'onBeginContact'; - break; - case Contact2DType.END_CONTACT: - func = 'onEndContact'; - break; - case Contact2DType.PRE_SOLVE: - func = 'onPreSolve'; - break; - case Contact2DType.POST_SOLVE: - func = 'onPostSolve'; - break; - default: - break; - } - const colliderA = this.colliderA; const colliderB = this.colliderB; - const bodyA = colliderA!.body; - const bodyB = colliderB!.body; + const hasListenerA = colliderA?.body?.enabledContactListener; + const hasListenerB = colliderB?.body?.enabledContactListener; - if (bodyA!.enabledContactListener) { - colliderA?.emit(contactType, colliderA, colliderB, this); + if (hasListenerA) { + colliderA.emit(contactType, colliderA, colliderB, this); } - if (bodyB!.enabledContactListener) { - colliderB?.emit(contactType, colliderB, colliderA, this); + if (hasListenerB) { + colliderB.emit(contactType, colliderB, colliderA, this); } - if (bodyA!.enabledContactListener || bodyB!.enabledContactListener) { + if (hasListenerA || hasListenerB) { PhysicsSystem2D.instance.emit(contactType, colliderA, colliderB, this); } diff --git a/cocos/physics-2d/box2d-wasm/physics-contact.ts b/cocos/physics-2d/box2d-wasm/physics-contact.ts index 2603f8887d..e884478da7 100644 --- a/cocos/physics-2d/box2d-wasm/physics-contact.ts +++ b/cocos/physics-2d/box2d-wasm/physics-contact.ts @@ -209,18 +209,18 @@ export class PhysicsContact implements IPhysics2DContact { const colliderA = this.colliderA; const colliderB = this.colliderB; - const bodyA = colliderA!.body; - const bodyB = colliderB!.body; + const hasListenerA = colliderA?.body?.enabledContactListener; + const hasListenerB = colliderB?.body?.enabledContactListener; - if (bodyA!.enabledContactListener) { - colliderA?.emit(contactType, colliderA, colliderB, this); + if (hasListenerA) { + colliderA.emit(contactType, colliderA, colliderB, this); } - if (bodyB!.enabledContactListener) { - colliderB?.emit(contactType, colliderB, colliderA, this); + if (hasListenerB) { + colliderB.emit(contactType, colliderB, colliderA, this); } - if (bodyA!.enabledContactListener || bodyB!.enabledContactListener) { + if (hasListenerA || hasListenerB) { PhysicsSystem2D.instance.emit(contactType, colliderA, colliderB, this); }