老师为什么我的子弹打到飞机都不爆炸呢?
来自课程:CocosCreator3.8入门教程-微信飞机大战
start() {
console.log('enemy start');
this.collider = this.getComponent(Collider2D); // 初始化collider
if (this.collider) {
this.collider.on(Collider2D.BEGIN_CONTACT, this.onBeginContact, this);
}
}
update(deltaTime: number) {
if (this.hp > 0) {
const { x, y, z } = this.node.position;
this.node.setPosition(x, y - this.speed * deltaTime, z);
}
if (this.node.position.y < -580) {
this.node.destroy();
}
}
onBeginContact() {
console.log('begin contact');
this.hp -= 1;
this.anim.play();
if (this.anim) { // 添加anim空值检查
this.anim.play();
}
if (this.hp <= 0 ) {
if (this.collider !== null) {
this.collider.enabled = false;
}
this.scheduleOnce(function (){
this.node.destroy();
}, 1);
}
}
碰撞器和刚体都和老师设置的一样

