egret.ImageLoader 使用範例
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
const imageLoader = new egret.ImageLoader;
imageLoader.crossOrigin = 'anonymous'; // 啟用跨域資源共享
imageLoader.addEventListener(egret.Event.COMPLETE, (e) => {
const texture = new egret.Texture();
texture.bitmapData = e.currentTarget.data;
this.addChild(new eui.Image(texture));
}, this);
imageLoader.load('https://www.google.com/pathToImage.png');
// 方法2,請用此方法取代上述方法,更為簡單
egret.ImageLoader.crossOrigin = 'anonymous'; // 全域參數啟用跨域資源共享
const tex = await RES.getResByUrl('https://www.google.com/pathToImage.png');
this.addChild(new eui.Image(tex));
}
}
沒有留言:
張貼留言