core-egret.Event

egret.Event 事件機制範例


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 給你最乾淨的程式碼, 從這行以下開始寫喔 ---

        // 參考 https://claire-chang.com/2013/08/22/as3的事件傳遞機制/

        // 建立紅色方塊並綁定點擊事件
        const left = new eui.Rect;
        left.width = 100;
        left.height = 100;
        left.x = 100;
        left.fillColor = 0xFF0000;
        this.addChild(left);
        left.addEventListener(egret.TouchEvent.TOUCH_TAP, function (event) {
            egret.log("點擊左側方塊,eventPhase:" + event.eventPhase);
        }, left);

        // 建立綠色方塊並綁定點擊事件
        const right = new eui.Rect;
        right.width = 100;
        right.height = 100;
        right.x = 145;
        right.y = -40;
        right.fillColor = 0x00FF00;
        this.addChild(right);
        right.addEventListener(egret.TouchEvent.TOUCH_TAP, function (event) {
            egret.log("點擊右側方塊,eventPhase:" + event.eventPhase);
        }, right);

        // Main 綁定點擊事件, 預設為冒泡監聽
        this.addEventListener(egret.TouchEvent.TOUCH_TAP, function (event) {
            egret.log("容器冒泡監聽,eventPhase:" + event.eventPhase);
        }, this);
        // 第四個參數 useCapture 設置為 true 捕獲階段
        this.addEventListener(egret.TouchEvent.TOUCH_TAP, function (event) {
            egret.log("容器捕獲監聽,eventPhase:" + event.eventPhase);
        }, this, true);
        
    }

}

spacer

沒有留言:

張貼留言