core-egret.EventDispatcher

egret.EventDispatcher 使用範例


class Main extends eui.UILayer {

    protected createChildren(): void {
        super.createChildren();
        egret.lifecycle.onPause = () => egret.ticker.pause();
        egret.lifecycle.onResume = () => egret.ticker.resume();
        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 給你最乾淨的程式碼, 從這行以下開始寫喔 ---

        // 自定義 click_finish 事件, 點擊10次後觸發
        const endTriggerTimes = 10;
        let currentTriggerTimes = 0;
        // 自定義觸發器
        const dispatcher: egret.EventDispatcher = new egret.EventDispatcher();
        dispatcher.addEventListener('click_finish', () => console.log('點擊完畢'), this);
        // 建立黑色方塊並設置點擊次數偵測方法
        const rect = this.addChild(new eui.Rect) as eui.Rect;
        rect.width = 100;
        rect.height = 100;
        rect.addEventListener(egret.TouchEvent.TOUCH_TAP, function () {
            if (++currentTriggerTimes === endTriggerTimes) {
                // arguments.callee 是指當下運行函式及目前函示, 在箭頭函示中無法使用
                rect.removeEventListener(egret.TouchEvent.TOUCH_TAP, arguments.callee, this);
                // 觸發點擊完成事件
                dispatcher.dispatchEventWith('click_finish');
            }
        }, this);

    }

}

spacer

沒有留言:

張貼留言