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

前端之家收集整理的这篇文章主要介绍了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,0);
          newbullet(px+10,0);
          break;
        case 2:
          newbullet(px-10,-0.2);
          newbullet(px,0.2);
        default:
          newbullet(px,0);
      }
    }

});
原文链接:https://www.f2er.com/cocos2dx/338530.html

猜你在找的Cocos2d-x相关文章