egret.EventPhase 使用範例
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 r = new eui.Rect;
r.width = 100;
r.height = 100;
this.addChild(r);
const listener = (event: egret.Event) => {
switch (event.eventPhase) {
case egret.EventPhase.CAPTURING_PHASE:
egret.log("容器捕獲,eventPhase:" + event.eventPhase);
break;
case egret.EventPhase.AT_TARGET:
egret.log("點擊方塊,eventPhase:" + event.eventPhase);
break;
case egret.EventPhase.BUBBLING_PHASE:
egret.log("容器冒泡,eventPhase:" + event.eventPhase);
break;
}
}
r.addEventListener(egret.TouchEvent.TOUCH_TAP, listener, this);
this.addEventListener(egret.TouchEvent.TOUCH_TAP, listener, this); // 預設是冒泡模式
this.addEventListener(egret.TouchEvent.TOUCH_TAP, listener, this, true); // 設成 true 是表示捕獲模式
}
}
沒有留言:
張貼留言