现在有子d了有敌机了 所以事情简单了,两者碰撞,都消失,真正做到一q消灭一个侵略者
先重看一下完整的场景
var HelloWorldScene = cc.Scene.extend({ onEnter:function () { this._super(); //两个背景层 var layer = new BGLayer(res.BACK4_2,res.BACK4_1,-2); this.addChild(layer,100) var bg2= new BGLayer(res.BACK3_1,res.BACK3_2,2); this.addChild(bg2,200); //敌人来了 var enemyLayer=new Enemy(); this.addChild(enemyLayer,250); //可以飞了 var airLayer=new Airplane(); this.addChild(airLayer,300); airLayer.enemyLayer=enemyLayer; }});
2更完整的airplane,其实与前面相比.只是多了一个update,用于碰撞检测
var Airplane = cc.Layer.extend({ ctor:function (){ var me=this; me._super(); var size=cc.winSize; me.air=new cc.Sprite(res.AIRPLAN); me.air.setposition(size.wIDth/2,size.height/2); me.addChild(me.air); me.batchNode=new cc.SpriteBatchNode(res.BulLET1); me.batchNode.retain(); me.bullteID=1; me.bullteSpeed=500; //有3种风格可以选择 me.bulletStyle=0; //添加子d风格,默认为0 var menu=new cc.Menu(new cc.MenuItemFont("子d风格",function(sender){ this.bulletStyle+=1; if(this.bulletStyle>2){this.bulletStyle =0} },me)); menu.setposition(size.wIDth - 100,15); me.addChild(menu) cc.eventManager.addListener(cc.EventListener.create({ event: cc.EventListener.touch_ONE_BY_ONE,swallowtouches: true,ontouchBegan: me.ontouchBegan.bind(me),ontouchmoved: me.ontouchmoved.bind(me),ontouchended: me.ontouchended.bind(me),}),me); me.schedule(me.fire,0.5); me.scheduleUpdate(); return true; },update:function( dt){ this.enemyLayer.children.forEach(function(enemy){ this.children.forEach(function(bullet){ var enemy=this; var eRect=enemy.getBounding@R_301_6951@(); var bRect=bullet.getBounding@R_301_6951@(); //本层中英雄没有tag,子d有 if(bullet.getTag()> 0 && cc.rectIntersectsRect(eRect,bRect)){ bullet.removeFromParent(); enemy.dropMe(); } }.bind(enemy)); }.bind(this)); },onExit:function(){ me.batchNode.release(); },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){ this.removeChildByTag(ID); },this,bID) ]); sp.runAction(action); }.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); } },ontouchBegan:function(touch,e){ var me=this; var size=me.air.getContentSize(); var rect = cc.rect(0,size.wIDth,size.height); var point = me.air.convertToNodeSpace(touch.getLocation()); if(cc.rectContainsPoint(rect,point)){ me.started=true; return true; } return false; },ontouchmoved:function(touch,e){ var me=this; var size=me.air.getContentSize(); var point = me.convertToNodeSpace(touch.getLocation()); if(!!me.started){ var x=point.x; var y=point.y; var between=function(v,max,min){ if(v<=min){return min;} else if(v >= max){return max;} else{return v;} }; me.air.x=between(point.x,cc.winSize.wIDth - size.wIDth /2,size.wIDth / 2); me.air.y=between(point.y,cc.winSize.height - size.height / 2,size.height / 2); } },ontouchended:function(t,e){ this.started=false; }});总结
以上是内存溢出为你收集整理的Cocos2d-x《雷电大战》(6) 一q消灭一个侵略者全部内容,希望文章能够帮你解决Cocos2d-x《雷电大战》(6) 一q消灭一个侵略者所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)