cocos 微信小飞机 (预设体、更换精灵、碰撞、监听事件、音乐)


  预设体  和 随机生成预设体 

@ccclass
export default class EnemyConfigContro extends cc.Component {

   @property(cc.Prefab)
   enemy: cc.Prefab=null;

   onLoad () {}

    start () {
         // 每2秒生成一个敌机
         this.schedule(()=>{
             let en=cc.instantiate(this.enemy);
             en.setParent(cc.director.getScene())
             en.y=this.node.y;
             en.x=Math.random()*400+20;
         },2)
    }

   update (dt) {}
}

死亡    加载图片 更换 精灵

//  自己死亡
       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);
       }

 碰撞

  //碰撞方法
       onCollisionEnter(other){
             // 自己销毁  和让敌军死亡
             if(other.tag==1){
                  // 说明是敌军 需要将敌军销毁
                  other.getComponent(EnemyContro).die();
                  //销毁自己
                  this.node.destroy();
             }
       }

//  区分碰撞对象是根据对象身上绑定的tag 来确定的

 

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

 碰撞需要开启碰撞检测 

监听事件

    start () {
          //绑定手指监听事件 
           this.node.on(cc.Node.EventType.TOUCH_MOVE,(event)=>{
              this.node.setPosition(event.getLocation());
           })

           //  攻击  每隔一秒的时间创建一个子弹  子弹是 预设体
           this.schedule(()=>{
              // 创建子弹
              let bullet=cc.instantiate(this.bulletPre);
                // 创建了预设体 第一步一定是设置父级
                bullet.setParent(cc.director.getScene())   // 父级是当前场景
              bullet.x=this.node.x;
              bullet.y=this.node.y+65;
           },1)

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

 

播放音乐

   cc.loader.loadRes("/music/y1271",cc.AudioClip,(res,clip)=>{
             // 赋值音频 
            
            player.clip=clip;
            player.play();  // 播放
            // player.stop();  //停止
            // player.isPlaying; // 是否正在播放
            // player.pause();  //暂停
            // player.resume(); //恢复
            player.loop=true;// 循环播放
            player.volume=1; //声音大小
         });
    // 加载音频  第二种方式
         cc.loader.loadRes("/music/y1271",cc.AudioClip,(res,clip)=>{
            // 赋值音频 
           let audioId=  cc.audioEngine.playMusic(clip,true);
            // 是否正在播放
            cc.audioEngine.isPlaying(); // 
            // // 停止
            // cc.audioEngine.stop(audioId);
            // //暂停
            // cc.audioEngine.pause(audioId);
            // //恢复
            // cc.audioEngine.resume(audioId);
            // //循环播放
            // cc.audioEngine.setLoop(audioId,true);
            // 声音大小
            cc.audioEngine.setVolume(audioId,1);    
        });

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值