Egret實戰開發筆記,飛行射擊遊戲(三)

今天是開發飛行射擊遊戲第三天,記錄工廠實現子彈體系——玩家子彈帶多樣性。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圖片壓縮的緣由,可能有一些卡頓失真,但實際效果不會卡頓,請原諒

至此,第三天的開發筆記已經完成,學習須要堅持,堅持到最後必定會有結果,天天寫下筆記來記錄本身的學習內容, 之後有須要也能夠查看,你們能夠一塊兒學習。

相關文章
相關標籤/搜索