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

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,py,0);
          newbullet(px+10,py,0);
          break;
        case 2:
          newbullet(px-10,py,-0.2);
          newbullet(px,py,0);
          newbullet(px+10,py,0.2);
        default:
          newbullet(px,py,0);
      }
    }

});


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值