版本 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();
}
}
});



