cocos creator 碰撞不生效问题

今天在b站学习飞羽老师的飞机大战的时候,发现子弹和敌机碰撞不生效。

研究了一下,发现是因为使用了EnemyManager去生成敌机,所以需要在每个敌机的onLoad的时候开启碰撞检测。

飞羽老师说在playerControl里面开启,但是好像是由于版本问题,没有生效。改成在EnemyControl中就可以了

代码如下

import BulletControl from "./BulletControl";

const {ccclass, property} = cc._decorator;

@ccclass
export default class EnemyControl extends cc.Component {

    // 是否死亡
    isDie: boolean = false;

    onLoad() {
        // 开启碰撞检测
        cc.director.getCollisionManager().enabled = true;
    }

    start () {

    }

    update (dt) {
        // 移动
        if (!this.isDie) {
            this.node.y -= 200 * dt;
        }
        // 移动出屏幕后销毁
        if (this.node.y < -850) {
            this.node.destroy()
        }

    }

    // 死亡
    die() {
        this.isDie = true;
        // 加载爆炸图片
        cc.loader.loadRes("enemy0_die", cc.SpriteFrame, (err, res) => {
            this.node.getComponent(cc.Sprite).spriteFrame = res;
        })
        // 300毫秒后销毁
        setTimeout(() => {
            this.node.destroy()
        }, 300);
    }
}

 

posted @ 2022-09-18 15:37  菲菲菲菲菲常新的新手  阅读(599)  评论(0编辑  收藏  举报