老师为什么我的子弹打到飞机都不爆炸呢?

梁国将

1 回复17 浏览

来自课程: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);

}

}

碰撞器和刚体都和老师设置的一样

 

 

全部回复(1)

  • 老师_Jokey 1 楼

    在碰撞后,是否有控制台的输出呢。单看代码不太能看出来,可以进群发图交流

发表回复

登录后可以回复