egret.sys.SystemTicker 使用範例
class Main extends eui.UILayer {
protected createChildren(): void {
super.createChildren();
// 不在生命週期管理 egret.sys.SystemTicker, 因此註解下面兩行程式
// 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 給你最乾淨的程式碼, 從這行以下開始寫喔 ---
// 本範例手動管理 egret.sys.SystemTicker
// egret.ticker 型別即 egret.sys.SystemTicker
// 建立動畫
const rect = new eui.Rect;
rect.width = rect.height = 100;
this.addChild(rect);
this.addEventListener(eui.UIEvent.ENTER_FRAME,()=>{
if(rect.x > 100) rect.x = 0;
rect.x += 2;
},this);
// 遊戲暫停按鈕
const btn1 = new eui.Button();
btn1.label = '遊戲暫停';
btn1.y = 120;
btn1.addEventListener(egret.TouchEvent.TOUCH_TAP,()=>egret.ticker.pause(),this);
this.addChild(btn1);
// 遊戲復原按鈕
const btn2 = new eui.Button();
btn2.label = '遊戲復原';
btn2.x = 100;
btn2.y = 120;
btn2.addEventListener(egret.TouchEvent.TOUCH_TAP,()=>egret.ticker.resume(),this);
this.addChild(btn2);
}
}
沒有留言:
張貼留言