Egret combat development notes, flying shooting game (3)

Today is the third day of development of a flying shooting game, and the factory implements a bullet system.

Introduction

Realize the diversity of player bullet belts.

Welcome everyone to study with me ~ The following is the detailed process code, as well as the errors encountered and how to correct them.

Achieve effect

Insert picture description here

Code and process

Flying Shooter Bullet Band Diversity

One. How to create more kinds of bullets?

Implement different bullets with category index id in ZD class

Apply in the ZD category

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);	
}

After construction, the ZDManager class will report an error, add the Id attribute in the create method

When calling the create method, add the bullet id

The core idea of ​​playing flying shooting is that players are hiding bullets.

Two: animation frame bullets (blinking bullets)

The blinking is because the coordinates move linearly during the update and the animation frame is switched

Loop animation implementation
Then how to switch the animation frame: need a timer, as the timer changes, and then change the texture, switch back and forth
2 pictures three times the main loop first, three times the main loop second, the cycle of a change is six times cycle. 3 pictures, one cycle is six times. And so on.

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 这个33次主循环变一帧,就是帧时长。

   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, a circle is 360 degrees, Math.random () * 360 achieves random firing of bullets between 0 ~ 360 degrees,

three. Implementation of multi-state bullets

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;	//跳出
		}

Because it is changed between 3 and 4, the number of (Math.random () * 2 + 3) (0 1) does not include 1, then * 2 is (the number between 0 2) +3 is rounded to 3 ~ 4

return; In some special codes, return after if, the condition is met, and the following code is not executed.

Four increase player launch method

Cut the update method public update () in Maingame to the Player class and add the fire method

//专用于发射子弹
	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;

       }
	}

Insert picture description here
Due to the compression of the gif image, there may be some stuttering distortion, but the actual effect will not stutter, please forgive me
so far, the third day of the development notes have been completed, learning needs to be persisted, and there must be results at the end, write down every day Take notes to record your learning content, you can also view it later if you need it, everyone can study together.

I want to learn together the public can focus on my number Shanghai Museum endless find me, exchange of learning, access to picture material and source code.

Published 4 original articles · won 36 · views 855

Guess you like

Origin blog.csdn.net/qq_39207481/article/details/105601708