cocos creator 3.8 碰撞检测问题

  • Creator 版本: 3.8.2

  • 目标平台: Edge

  • 重现方式:
    我在做一个2d横板游戏,想让角色触碰到地面时候,发生碰撞检测回调。我给角色设置了collider2d 和 rigidbody2d,给地面增加了collider2d组件。然后我勾选了角色的rigidbody2d的Enabled Contact Listener的选项。在角色的脚本中添加了如下代码:

`
import { _decorator, Component, input, Input, Node, EventKeyboard,KeyCode, RigidBody2D, Vec2, Vec4, Collider2D, Contact2DType, IPhysics2DContact } from ‘cc’;
const { ccclass, property } = _decorator;

@ccclass(‘Movement’)
export class Movement extends Component {
@property({type: Number})
speed: number = 100;

velocity: Vec2 = new Vec2(0, 0); 


rb2d: RigidBody2D | null  = null;
// collider2d: Collider2D | null = null;

direction: Vec2 = new Vec2(0, 0);

jumpCount: number = 2;




start() {
    this.rb2d = this.node.getComponent(RigidBody2D);
    const collider2d = this.node.getComponent(Collider2D);
    input.on(Input.EventType.KEY_DOWN, this.onKeyDown, this);
    collider2d.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
    collider2d.on(Contact2DType.END_CONTACT, this.onEndContact, this);
}

onKeyDown(key: EventKeyboard ) {
    switch(key.keyCode)
    {
        case KeyCode.ARROW_UP:
        case KeyCode.KEY_W:
            if(this.jumpCount)
            {
                this.rb2d.applyLinearImpulseToCenter(new Vec2(0, this.speed), true);
                this.jumpCount--;
            }
            
            // this.velocity.y = this.speed;
            break;
        case KeyCode.ARROW_RIGHT:
        case KeyCode.KEY_D:
            // this.velocity.x = this.speed;

            this.rb2d.applyLinearImpulseToCenter(new Vec2(this.speed, 0), true);
            break;
        case KeyCode.ARROW_DOWN:
        case KeyCode.KEY_S:
            // this.rb2d.applyLinearImpulseToCenter(new Vec2(0, this.speed), true);
            break;
        case KeyCode.ARROW_LEFT:
        case KeyCode.KEY_A:
            // this.velocity.x = -this.speed;
            this.rb2d.applyLinearImpulseToCenter(new Vec2(-this.speed, 0), true);
            
            break;
    }
    // this.rb2d.linearVelocity = this.velocity;

    
}

update(deltaTime: number) {
    // console.log(this.rb2d.linearVelocity)
}

onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
    console.log(123)
}
onEndContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
    console.log(123)
}

}

`

可是在浏览器中就报错了,提示Cannot read properties of null (reading ‘enabledContactListener’)

  • 首个报错:

    physics-contact.ts:242 Uncaught TypeError: Cannot read properties of null (reading ‘enabledContactListener’)
    at PhysicsContact.emit (physics-contact.ts:242:20)
    at PhysicsContactListener._onPreSolve [as _PreSolve] (physics-world.ts:417:11)
    at PhysicsContactListener.PreSolve (physics-contact-listener.ts:76:18)
    at b2PolygonContact.Update (box2d.umd.js:11809:24)
    at b2ContactManager.Collide (box2d.umd.js:12479:17)
    at b2World.Step (box2d.umd.js:19588:33)
    at b2PhysicsWorld.step (physics-world.ts:162:21)
    at PhysicsSystem2D.postUpdate (physics-system.ts:295:31)
    at Director.tick (director.ts:692:38)
    at Game._updateCallback (game.ts:1039:22)

我自己搞通了,地面也要加rigidbody2d