egret.Stage 使用範例
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 給你最乾淨的程式碼, 從這行以下開始寫喔 ---
// stage 是唯一且最高的根節點, 為 Main 的父節點
// stage 取得方法有兩種
// 1.透過單例
const stage = egret.MainContext.instance.stage;
// 2.DisplayObject 都有 stage 屬性, 但是必須加到場景後才能拿到 stage
const rect = new eui.Rect;
console.log(rect.stage); // null
this.addChild(rect);
console.log(rect.stage); // Stage {....}
this.removeChild(rect);
console.log(rect.stage); // null
// 可以用 xxx.stage 來判斷該物件是否在場景上
if(rect.stage) console.log('物件在場景樹裡')
}
}
沒有留言:
張貼留言