加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Cocos2d-x《雷电大战》(4)-策略模式实现不同子弹切换!!

发布时间:2020-12-14 17:23:41 所属栏目:百科 来源:网络整理
导读:Cocos2d-x《雷电大战》(4)-策略模式实现不同子弹切换!! 一种子弹不过瘾 多来几种?我们准备了一个风格切换按钮,然后改写了fire var Airplane = cc.Layer.extend({ ctor:function (){//..... //有3种风格可以选择 me.bulletStyle=0; //添加子弹风格,默认为0

Cocos2d-x《雷电大战》(4)-策略模式实现不同子弹切换!!

一种子弹不过瘾 多来几种?我们准备了一个风格切换按钮,然后改写了fire

var Airplane = cc.Layer.extend({
    ctor:function (){
//.....

      //有3种风格可以选择
      me.bulletStyle=0; //添加子弹风格,默认为0
      var menu=new cc.Menu(new cc.MenuItemFont("子弹风格",function(sender){
        this.bulletStyle+=1;
        if(this.bulletStyle>2){this.bulletStyle =0}
      },me));
      menu.setPosition(size.width - 100,15);
      me.addChild(menu)
//....
      return true;
    },fire:function(dt){
      var me=this;
      var point=me.air.getPosition();

      var newbullet=function(px,py,dx){
        var sp=new cc.Sprite(this.batchNode.getTexture());
        sp.setPosition(px,py);
        var bid=this.bullteId++;
        sp.setTag(bid);
        this.addChild(sp,-1);
        var flyLen= cc.winSize.height - py;
        var duration = flyLen / this.bullteSpeed;
        var k= (cc.winSize.height-py)*dx
        var action=new cc.Sequence([
          new cc.MoveTo(duration,cc.p(px + k,cc.winSize.height )),new cc.CallFunc(function(bullet,id){
            delete this.bullets[id];
            this.removeChildByTag(id);
          },this,bid)
        ]);
        sp.runAction(action);
        this.bullets[bid]=sp;
      }.bind(me);

      var px=point.x;
      var py=point.y + me.air.getContentSize().height + 20;
      switch(me.bulletStyle){
        case 1:
          newbullet(px-10,0);
          newbullet(px+10,0);
          break;
        case 2:
          newbullet(px-10,-0.2);
          newbullet(px,0.2);
        default:
          newbullet(px,0);
      }
    }

});

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读