好象掉了单例 算了与cocos2d-x也没关系 最后我加上消灭敌人算补偿?
有两种敌人会定期的循环出现,现个调试器实现
1 资源
var res = { //... AIR3:'res/air3.png',AIR4:'res/air4.png',//.... };2HelloWorldScene.onEnter
var HelloWorldScene = cc.Scene.extend({ onEnter:function () { //.... //敌人来了 var enemyLayer=new Enemy(); this.addChild(enemyLayer,250); //.... } });
3新的脚本资源project.json
"jsList" : [ //.. "src/enemy.js",//.. ]
var Enemy = cc.Layer.extend({ flyEnemy:function(dt){ var me=this; var newfly=function(sprite,p){ var tag=this.enemyCount++; //加入到场景 sprite.setPosition(p.x,p.y); sprite.setRotation(180); sprite.setScale(1.5); sprite.setTag(tag); this.addChild(sprite); //移动到底 var speed=200; var height= sprite.getContentSize().height / 2; var duration= (p.y + height) / speed; var moveto=new cc.MoveTo(duration,cc.p(p.x,-height)); sprite.dropMe=me.dropEnemy.bind(sprite); var dropMe=new cc.CallFunc(sprite.dropMe,sprite); sprite.runAction(new cc.Sequence([moveto,dropMe])); }; var size=cc.winSize; var sprite=new cc.Sprite(this.air4SBN.getTexture()); var spriteSize=sprite.getContentSize(); newfly.call(me,sprite,cc.p(size.width/2,size.height-spriteSize.height)); newfly.call(me,new cc.Sprite(this.air4SBN.getTexture()),cc.p(size.width/2-spriteSize.width * 2,size.height+spriteSize.height)); newfly.call(me,cc.p(size.width/2+spriteSize.width * 2,size.height+spriteSize.height)); },dropEnemy:function(){ var sp=this; var tag=sp.getTag(); sp.removeFromParent(); cc.log("remove enemy %d",tag); },newEnemy:function(dt){ //加一架飞机 var me=this; var enemy=new cc.Sprite(this.air3SBN.getTexture()); var eid=me.enemyCount++; enemy.setRotation(90); var y=cc.winSize.height / 3 * 2; var x=cc.winSize.width / 2; enemy.setPosition(0,y); enemy.setScale(0.25); enemy.setTag(eid); me.addChild(enemy) //会动 p1:[250,300],p2:[180,150],p3[0,10] var bezierTo=new cc.BezierTo(3,[cc.p(x,y+100),cc.p(x,y / 2 -100),cc.p(0,y / 2)]); //会转 var rotateBy=new cc.RotateBy(3,180); //同时 var spawn=new cc.Spawn(bezierTo,rotateBy); //负责销毁自己 enemy.dropMe=me.dropEnemy.bind(enemy); var dropMe=new cc.CallFunc(enemy.dropMe,enemy); var sequence=new cc.Sequence([ spawn,dropMe]); enemy.runAction(sequence); },ctor:function(){ var me=this; me._super(); me.enemyCount=1; me.air3SBN=new cc.SpriteBatchNode(res.AIR3); me.air3SBN.retain(); me.air4SBN=new cc.SpriteBatchNode(res.AIR4); me.air4SBN.retain(); //两种敌人 me.schedule(me.newEnemy,0.5); me.schedule(me.flyEnemy,2); //ctor中两者都可以使用 onEnter中则只能使用后者了? //cc.director.getScheduler().schedule(me.newEnemy,me,0.5); return true; },onExit:function(){ me.air3SBN.release(); me.air4SBN.release(); } })