1. 程式人生 > >《菜鳥教程》| Egret場景切換管理類切換和單例使用方法

《菜鳥教程》| Egret場景切換管理類切換和單例使用方法

場景切換是很多開發者在開發過程中必不可少的一個環節,當專案中有兩個或兩個以上的遊戲場景時,怎樣管理這些場景,能夠使它們之間的切換更加方便呢?今天就為大家介紹場景切換管理類的切換方法和單例的使用方法。

案例原始碼:https://github.com/hkjlx/qhcj

首先建立一個所有場景的父類Scene

Scene類主要是為了方便管理場景,此類是一個抽象類,子類必須繼承此類並實現onComplete()抽象方法才能進行場景的切換。


abstract class Scene extends eui.Component{
    public constructor() {
        super();
        // 監聽元件建立完畢 也就是場景的外觀建立完畢
        this.addEventListener(eui.UIEvent.CREATION_COMPLETE,this.onComplete,this);
    }
     protected abstract onComplete();
}

場景管理器SceneManger

所有場景的切換,彈出關閉都是由SceneManger類來控制,這樣方便對場景進行統一管理。

1.使用單例模式

SceneManger類需要使用到單例模式,單例模式是一種常用的軟體設計模式,其定義是單例物件的類只能允許一個例項存在。

class SceneManager {
    private static _manager:SceneManager;
    public static get Instance(){
        if( SceneManager._manager==null){
            SceneManager._manager = newSceneManager();
        }
        return SceneManager._manager;
    }
    public constructor() {
    }
}

2.控制場景切換的方法

changeScene()方法:當舞臺上有場景時,會先將當前場景從舞臺移除,再新增新場景到舞臺上;當舞臺還沒有場景時,會直接新增到舞臺上。

  public rootLayer:eui.UILayer;//起始場景
    private currentScene:Scene;//需要顯示的場景
    private pop_scene:Scene;//彈出場景層
    //切換場景
    public changeScene(s:Scene){
        if(this.currentScene){
           this.rootLayer.removeChild(this.currentScene);
            this.currentScene = null;
        }
        this.rootLayer.addChild(s);
        this.currentScene = s;
    }

3.彈出場景和關閉彈出場景

彈出場景不會使底層場景消失,而是直接在當前場景上再顯示一個場景出來(主要用於設定面板之類的)。

在彈出場景時先呼叫了一次關閉場景層,防止還沒關閉場景層又點選了彈出場景層。

  //彈出場景層
    public pushScene(s:Scene){
        this.popScene();
        if(!this.pop_scene){
            this.rootLayer.addChild(s);
            this.pop_scene = s;
        }
    }
    //關閉場景層
    public popScene(){
        if(this.pop_scene){
            this.rootLayer.removeChild(this.pop_scene);
            this.pop_scene = null;
        }
    }

在入口檔案 Main.ts 中引入場景管理類 SceneManage

首先將Main.ts中createGameScene()方法中的程式碼刪掉,再呼叫下面的方法,將this定為起始場景(舞臺)。

SceneManager.Instance.rootLayer = this;

使用eui建立三個場景和一個彈出場景

使用eui建立三個場景,分別為:開始場景(StartScene.exml),遊戲場景(GameScene.exml),結束場景(EndScene.exml)。

下圖示例為開始場景(StartScene.exml):

使用eui建立彈出層(TanChu.exml)。

下圖示例為彈出場景:

eui檔案建立完成之後需在終端中輸入egret build編譯專案,這時會在default.thm.json 檔案中生成面板對應的skinName。

開始場景(StartScene.ts)、遊戲場景(GameScene.ts),結束場景(EndScene.ts)對應的TS檔案

由於StartScene類繼承自Scene抽象類,所以在此類中必須得實現onComplete()方法,示例程式碼如下:

class StartScene extends Scene {
        public btn_tc: eui.Label;//彈出層按鈕
        public btn_qh2: eui.Label;//切換場景


        public constructor() {
            super();
            //指定開始場景對應的面板檔案StartScene.exml
            this.skinName ="resource/game/StartScene.exml";
        }
        //實現父類的onComplete方法
        protected onComplete() {
            //設定兩個Label為可點選
            this.btn_tc.touchEnabled =true;
            this.btn_qh2.touchEnabled =true;
            //新增點選事件
           this.btn_tc.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onTaptc,this);
            this.btn_qh2.addEventListener(egret.TouchEvent.TOUCH_TAP,this.onTapqiehuan, this);
        }
        //彈出場景
        private onTaptc(){
        }
        private onTapqiehuan(){
        }
}

遊戲場景(GameScene),結束場景(EndScene)對應的ts檔案基本與開始場景一致。

可參考原始碼:https://github.com/hkjlx/qhcj

遊戲初始化時在Main.ts中的createGameScene()方法新增開始遊戲場景(StartScene),切換場景時呼叫

SceneManager.Instance.changeScene()

即可;注意:此處引數為一個場景例項。

let s1:StartScene =  new StartScene();
SceneManager.Instance.changeScene(s1);

彈出場景層方法

在對應的點選事件中呼叫pushScene()方法。

let tc:Tanchu = new Tanchu();
SceneManager.Instance.pushScene(tc);//新增場景彈出層

如果需要關閉彈出場景層,在彈出場景層的類中呼叫popScene()方法。

SceneManager.Instance.popScene();//移除場景層

小結

本文主要講解了場景切換管理類的切換方法和單例的使用方法,有任何技術問題或者覺得這篇文章對你有所幫助,歡迎留言與我們交流互動!