step1. 請建立 game.xml 檔
<?xml version="1.0" encoding="utf-8" ?>
<game>
<name>Edwin Game</name>
<stage width="100" height="100"></stage>
</game>
step2. 將 game.xml 拉到資源庫, 請注意文件型態要 Text 而不是 bin
step3. 撰寫程式碼
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 給你最乾淨的程式碼, 從這行以下開始寫喔 ---
// game.xml
`
<?xml version="1.0" encoding="utf-8" ?>
<game>
<name>Edwin Game</name>
<stage width="100" height="100"></stage>
</game>
`;
// 獲取 xml 字串
const source = RES.getRes('game_xml');
// 獲取 egret.XML 物件
let game: egret.XML = egret.XML.parse(source);
// nodeType 節點類型,1:XML,2:XMLAttribute,3:XMLText (官方沒有實作 XMLAttribute)
// 根元素 game 標籤
egret.log(game.name, game.nodeType); // game 1
console.log('子元素有:', game.children.length,'個'); // 2
// 取第 1 個 name 標籤元素
let gameName: egret.XMLNode = game.children[0];
let nameText: egret.XMLText = gameName.children[0];
egret.log(gameName.name, gameName.nodeType); // name 1
egret.log(nameText.nodeType, nameText.text); // 3 "Edwin Game"
// 取第 2 個 stage 標籤元素
let gameStage: egret.XMLNode = game.children[1];
egret.log(gameStage.name, gameStage.nodeType); // stage 1
egret.log(gameStage.attributes); // {width: "100", height: "100"}
// xml 讀進的數值是字串, 請使用 parseInt 或 parseFloat 將自傳轉成數值
egret.log(parseInt(gameStage.attributes.width), parseInt(gameStage.attributes.height)); // 100 100
}
}
// 防止報錯
declare namespace egret {
interface XML{
children: egret.XMLNode[];
}
interface XMLNode {
name: string;
children: any[];
attributes: any; // 應為 egret.XMLAttribute
}
}
沒有留言:
張貼留言