egret.URLLoader 使用範例
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 給你最乾淨的程式碼, 從這行以下開始寫喔 ---
// egret 的資源讀取方法
// 1. egret.HttpRequest 為 xhr 的包裝類
// 2. egret.ImageLoader 為圖片讀取類
// 3. egret.URLLoader 為 egret.HttpRequest 的包裝類
// 4. RES.getRes 筆者推薦使用
const loader = new egret.URLLoader;
loader.dataFormat = egret.URLLoaderDataFormat.TEXTURE; // 材質
loader.addEventListener(egret.Event.COMPLETE, (e: egret.Event) => {
var texture = e.target.data as egret.Texture;
this.addChild(new eui.Image(texture));
}, this);
loader.load(new egret.URLRequest("resource/assets/Slider/thumb.png"));
const loader2 = new egret.URLLoader;
loader2.dataFormat = egret.URLLoaderDataFormat.TEXT; // 文本
loader2.addEventListener(egret.Event.COMPLETE, (e: egret.Event) => {
var text: string = e.target.data;
(this.addChild(new eui.Label) as eui.Label).text = text;
}, this);
loader2.load(new egret.URLRequest("resource/config/description.json"));
const loader3 = new egret.URLLoader;
loader3.dataFormat = egret.URLLoaderDataFormat.SOUND; // 音樂
loader3.addEventListener(egret.Event.COMPLETE, (e: egret.Event) => {
var snd = e.target.data as egret.Sound;
snd.play(); // 瀏覽器限制, 播放前請至少點擊一次遊戲畫面此方法才能生效
}, this);
loader3.load(new egret.URLRequest("resource/assets/bg.mp3"));
}
}
沒有留言:
張貼留言