今天是开发飞行射击游戏第三天,记录工厂实现子弹体系——玩家子弹带多样性。dom
实现效果:学习
在ZD类用种类索引id实现不一样的子弹动画
在ZD类申请this
public id:number; //子弹种类索引 构造 this.id = id; switch(this.id){ case 0 : this.im = Main.createBitmapByName("pzd2_1_png"); this.addChild(this.im); break; case 1: this.im = Main.createBitmapByName("pzd2_11_png"); this.addChild(this.im); }
构造完以后,ZDManager类会报错,在create方法中添加Id属性spa
调用create方法时,添加子弹id就能够了3d
玩飞行射击最核心的理念是玩家在躲子弹。code
二:动画帧子弹实现(闪烁子弹)blog
闪烁是由于在更新中坐标进行直线运动还有动画帧的切换索引
循环动画实现游戏
那么动画帧怎样切换:须要计时器,随着计时器改变,继而改变贴图,来回切换
2张图片三次主循环第一张,三次主循环第二张,一次变换的周期是六次主循环。3张图片,一次周期是六次。以此类推。
update方法中 if(this.id ==2){ this.t++; if(this.t >= 6){ //这个6是帧数*帧时长。 this.t = 0; //图片会变,因此t在0。1,2时除以3是0.几+3取整为3,t为3,4,5时除以3为1.几+3取整为4 //这样结果就为3和4之间变化。图片名为3和4 this.im.texture = RES.getRes("pzd2_"+Math.floor(this.t/3 + 3)+"_png"); } } this.t/3 这个3是3次主循环变一帧,就是帧时长。 for(let i = 0 ; i < 10 ; i++){ this.zm.create(2,this.player.x,this.player.y,15,Math.random()*360 , this ); }
Math.random()*360 , 一圈是360度,Math.random()*360 实现0~360度之间子弹随机发射.
public m:number; //子弹状态索引 //导弹 case 3: this.im = Main.createBitmapByName("pzd1_3_png"); this.addChild(this.im); this.t = 0; this.m = 0; break; update()最开始的方法 if(this.id == 3){ this.im.texture = RES.getRes("pzd1_"+Math.floor(Math.random()*2 + 3)+"_png"); switch(this.m ){ //初始角度更新10次主循环 case 0 : this.t++; this.x+=this.vx; this.y+=this.vy; if(this.t >=10){ this.t =0; this.m =1; } break; //发出后停滞一段时间 case 1: this.t++; if(this.t >=5){ this.t =0; this.m =2; this.vy = 0; } break; //向上加速运动 case 2: this.y+=this.vy; this.vy -=2; //这段是独立代码,因此须要检测出屏子弹消失 if(this.y <-100) this.vis = false; break; } return; //跳出 }
由于是3和4之间改变,因此(Math.random()*2 + 3) (01)的数不包括1,而后*2就是(02之间数)+3取整就是3~4
return;在某些特殊代码 ,在if后return,条件知足,后面代码不执行。
四 增长玩家发射方法
把Maingame中的更新方法public update()剪切到Player类中,添加fire方法
//专用于发射子弹 public fire(){ this.t++; //每4次主循环发射一颗 if(this.t %4 == 0){ this.game.zm.create(1,this.x,this.y,20,0,this.game); } if(this.t >= 15){ this.game.zm.create(3,this.x,this.y,10,210,this.game); this.game.zm.create(3,this.x,this.y,10,150,this.game); this.t = 0; } }
由于gif图片压缩的缘由,可能有一些卡顿失真,但实际效果不会卡顿,请原谅
至此,第三天的开发笔记已经完成,学习须要坚持,坚持到最后必定会有结果,天天写下笔记来记录本身的学习内容, 之后有须要也能够查看,你们能够一块儿学习。