eui-eui.Watcher

eui.Watcher 使用範例


class Main extends eui.UILayer {

    // 遊戲暫停狀態紀錄
    private isGamePaused: boolean = false;

    protected createChildren(): void {
        super.createChildren();
        // 遊戲狀態改變
        egret.lifecycle.onPause = () => { egret.ticker.pause(); this.isGamePaused = true };
        egret.lifecycle.onResume = () => { egret.ticker.resume(); this.isGamePaused = false };
        egret.registerImplementation('eui.IAssetAdapter', new AssetAdapter());
        egret.registerImplementation('eui.IThemeAdapter', new ThemeAdapter());
        this.runGame();
    }

    private async loadResource() {
        let loadingView = this.stage.addChild(new LoadingUI()) as LoadingUI;
        await RES.loadConfig('resource/default.res.json', 'resource/');
        await new Promise(resolve => new eui.Theme('resource/default.thm.json', this.stage).once(eui.UIEvent.COMPLETE, resolve, this));
        await RES.loadGroup('preload', 0, loadingView);
        this.stage.removeChild(loadingView);
    }

    private async runGame() {
        await this.loadResource();
        // --- Edwin 給你最乾淨的程式碼, 從這行以下開始寫喔 ---

        // Watcher 用以監聽屬性狀態
        eui.Watcher.watch(this, ['isGamePaused'], (paused) => {
            if(paused){
                console.log('遊戲暫停囉');
            }else{
                console.log('遊戲繼續囉');
            }
        }, this);

    }

}

spacer

沒有留言:

張貼留言