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); } } });
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。