碰撞事件不触发

版本 2.3.2 自己没看出来有什么问题,就是不能触发onCollisionEnter方法




// Learn cc.Class:
// - cc.Class · Cocos Creator
// Learn Attribute:
// - Attributes · Cocos Creator
// Learn life-cycle callbacks:
// - Lifecycle Callback · Cocos Creator

cc.Class({
extends: cc.Component,
properties: {
v: {
default: 300,
type: cc.Float,
tooltip: ‘速度’,
},
camera: {
default: null,
type: cc.Node,
tooltip: ‘控制相机’,
},
target: {
default: null,
type: cc.Node,
tooltip: ‘攻击对象’,
},
bullet: {
default: null,
type: cc.Node,
tooltip: ‘子弹模板’
},
running: {
default: false,
type: cc.Boolean,
tooltip: ‘是否在播放’
}
},

onLoad () {
    this.initState();
},

initState() {
    this.rigidBody = this.getComponent(cc.RigidBody);
    this.animation = this.getComponent(cc.Animation);
    this.setCamera(); // 初始化相机位置
    const manager = cc.director.getCollisionManager()
    manager.enabled = true; // 开启碰撞检测系统
    manager.enabledDebugDraw = true;
    manager.enabledDrawBoundingBox = true;
},

run() {
    if (this.running) return;
    this.running = true;
    this.animation.enabled = true;
},

stop() {
    this.running = false;
    this.animation.enabled = false;
},

// 初始化相机位置
setCamera() {
    if (this.camera) {
        this.camera.x = this.node.x;
        // this.camera.y = this.node.y;
    }
},

// 旋转
rotate(value) {
    this.node.angle = value;
},

// 设置速度
setV(type, number) {
    if (type === 'x' || type === 'y') {
        const rigidBody = this.rigidBody || this.getComponent(cc.RigidBody);
        const {linearVelocity} = rigidBody;
        linearVelocity[type] = number;
        rigidBody.linearVelocity = linearVelocity;
    }
},

onCollisionEnter(other, self) {
    alert('落地!');
    console.log('落地');
},

update() {
    if (this.rigidBody) {
        if (this.rigidBody.linearVelocity.x || this.rigidBody.linearVelocity.y) this.setCamera();
    }
}

});

没有人来看一下么?

this.rigidBody = this.getComponent(cc.RigidBody);
组件是在节点上面的,需要加上node
this.rigidBody = this.node.getComponent(cc.RigidBody);

而且你这个监听的好像不是物理碰撞吧
开启的也不是物理碰撞,文档里面有物理碰撞,可以先去看看

这两个其实是一样的。。。

1赞

至于题主的问题,我建议你可以写一个最小化 demo 出来,大佬们看一下就知道原因了。

不是那里报错,我换成 onBeginContact 事件就好了,不清除什么原因