egret.StageOrientationEvent 判斷橫屏幕或豎屏幕範例
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 給你最乾淨的程式碼, 從這行以下開始寫喔 ---
// 範例 1. 直接程式裡修改旋轉模式
this.stage.orientation = egret.OrientationMode.AUTO;
// 範例 2. 另外想判斷橫豎屏幕以下是變通方法
// 注意, 只有 this.stage 註冊才有效, 不能用 this 或任何非 stage 物件註冊
this.stage.addEventListener(egret.StageOrientationEvent.ORIENTATION_CHANGE,(e:egret.StageOrientationEvent)=>{
// 判斷橫屏幕或豎屏幕
const orientation = window.innerHeight > window.innerWidth ? egret.OrientationMode.PORTRAIT : egret.OrientationMode.LANDSCAPE;
console.log(orientation); // portrait or landscape
},this);
}
}
沒有留言:
張貼留言