菜鳥教程丨Egret製做Loading頁面及分步加載資源教程

咱們都知道,當遊戲越作越大,資源愈來愈多的時候,加載資源會形成大量時間的浪費。爲避免加載資源時遊戲黑屏,致使玩家誤認爲遊戲非正常運行,Loading界面起到相當重要的做用。今天就爲你們帶來用Egret製做Loading頁面及分步加載資源的教程。javascript

本文涉及如下內容:java

  • RES加載Loading界面所使用的資源
  • 分步加載資源

加載LoadingUI所須要的資源git

把LoadingUI所須要的資源配置到default.res.json的loading組中,組名任意。以下:github

 在Main.ts修改loadResource()函數資源的加載順序,先把LoadingUI所須要的資源異步加載成功,再建立LoadingUI的實例。json

privateasyncloadResource() {
        try{
            awaitRES.loadConfig("resource/default.res.json", "resource/");//加載配置表
            awaitRES.loadGroup("loading");//加載loading組
            constloadingView=newLoadingUI();//建立loadingUI實例
            this.stage.addChild(loadingView);
            awaitRES.loadGroup("preload", 0, loadingView);//加載默認preload組資源,並執行loadingView
            this.stage.removeChild(loadingView);
        }
        catch(e) {
            console.error(e);
        }
   }
​

 如此,資源配置完畢以後就能夠在LoaingUI中使用了,下面製做LoaingUI界面異步

製做LoadingUI界面async

本文事例中顯示資源真實加載百分比和圖片百分比,參照以下LoadingUI代碼函數

classLoadingUIextendsegret.SpriteimplementsRES.PromiseTaskReporter{
​
    publicconstructor() {
        super();
        this.createView();
   }
    /**百分比位圖*/
    privatetextField: egret.BitmapText;
    /**loadicon */
    privateload:egret.Bitmap;
    /**百分比圖片*/
    privateloadBar:egret.Bitmap;
    /**loadBar背景*/
    privateloadBar2:egret.Bitmap;
​
    privatecreateView(): void{
        this.textField=newegret.BitmapText();
        letfnt=RES.getRes("num_fnt");//加載字體位圖
        this.textField.text="0%";
        this.textField.font=fnt;
        this.textField.textAlign="center",
        this.textField.x=260,
        this.textField.y=550,
        this.textField.width=130,
        this.textField.height=100;
​
        letbg:egret.Bitmap=newegret.Bitmap(RES.getRes("loadingBG_jpg"));
        this.addChild(bg);
        this.load=newegret.Bitmap(RES.getRes("loading_json.loading_icon_png"));
        this.load.anchorOffsetX=this.load.width/2;
        this.load.anchorOffsetY=this.load.height/2;
        this.load.x=640/2;
        this.load.y=1136/2;
        this.addChild(this.load);
​
        this.loadBar2=newegret.Bitmap(RES.getRes("loading_json.loading_bar1_png"));
        this.loadBar2.x=(640-this.loadBar2.width) /2;
        this.loadBar2.y=(1136-this.loadBar2.height) /2;
        this.addChild(this.loadBar2);
​
        this.loadBar=newegret.Bitmap(RES.getRes("loading_json.loading_bar2_png"));
        this.loadBar.x=(640-this.loadBar.width) /2;
        this.loadBar.y=(1136-this.loadBar.height) /2;
        this.addChild(this.loadBar);
   }
    publiconProgress(current: number, total: number): void{
        /**顯示百分比*/
        this.textField.text=Math.ceil((current/total)*100).toString() +"%";
        //遮罩
        letmask=this.getSectorProgress(Math.ceil((current/total) *360));
        this.addChild(mask)
        this.loadBar.mask=mask;
        this.addChild(this.textField);
   }
    /**loadBar遮罩*/
    privategetSectorProgress(angle: number):egret.Shape{
            varself=this;
            varshape:egret.Shape=newegret.Shape();
            changeGraphics(angle);
        returnshape;
        //繪製shape遮罩
        functionchangeGraphics(angle) {
            shape.graphics.clear();
            shape.graphics.beginFill(16711680);
            shape.graphics.moveTo(self.loadBar.x, self.loadBar.y);
            shape.graphics.lineTo(self.loadBar.x+self.loadBar.width/2, self.loadBar.y+self.loadBar.height/2);
            shape.graphics.drawArc(self.loadBar.x+self.loadBar.width/2, self.loadBar.y+self.loadBar.height/2, self.loadBar.width/2, 0, angle*Math.PI/180);
            shape.graphics.lineTo(self.loadBar.x+self.loadBar.width/2, self.loadBar.y+self.loadBar.height/2);
            shape.graphics.endFill();
        }
   }
}

 LoadingUI製做完畢,如今運行,便可看到效果。測試

 

 分步加載資源字體

分步加載資源和LoadingUI加載方式相同,也一樣是爲了不一次性加載太多的資源而形成時間的浪費,加載的同時也能夠運行LoadingUI。在資源配置表中繼續增長資源組testRES,多加一些preload和loading以外的資源,效果更佳明顯。

在Main.ts中測試分步加載資源,原有的頁面上加上一個按鈕,添加加載資源事件。

   //跳轉場景加載資源測試
        lettextBtn: egret.TextField=newegret.TextField();
        textBtn.text="Click! 跳轉場景";
        textBtn.touchEnabled=true;
        textBtn.x=300;
        textBtn.y=500;
        this.addChild(textBtn);
        textBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onTextBtnClick, this);
​
    privateasynconTextBtnClick() {
        constloadingView=newLoadingUI();//建立loadingUI實例
        this.stage.addChild(loadingView);
        awaitRES.loadGroup("testRES", 0, loadingView);//加載默認preload組資源,並執行loadingView
        this.stage.removeChild(loadingView);
        this.addChild(newTestPanel());
   }

 按鈕方法關鍵詞async,使用異步加載執行此事件。測試分步加載資源TestPanel類

classTestPanelextendsegret.Sprite{
   publicconstructor() {
       super();
       this.init();
   }
​
   privateinit() {
        //原有資源
       letbg: egret.Bitmap=newegret.Bitmap( RES.getRes("loadingBG_jpg"));
       this.addChild(bg);
        //testRES組新的資源
       leticon_1: egret.Bitmap=newegret.Bitmap(RES.getRes("sjdj_bg2_png"));
       this.addChild(icon_1);
   }
}

 小結:

經過本文您能夠學到Loading頁面製做方法以及資源分步加載思想,有任何技術問題或者認爲這篇文章對您有所幫助,歡迎在評論區留言與咱們交流互動!

本文源碼素材連接:https://github.com/shenysun/LoadingT

相關文章
相關標籤/搜索