我无法从我的xml文件中获取值
XML文件(db.xml)
<?xml version="1.0" encoding="utf-8"?>
<character>
<main hp="100" lvl="10" dmg="5" def="5" exp="0.00" gold="10"/>
</character>
我的AS3代码
var myXML:XML;
var myLoader:URLLoader = new URLLoader();
myLoader.load(new URLRequest("db.xml"));
myLoader.addEventListener(Event.COMPLETE, processXML);
function processXML(e:Event):void {
myXML = new XML(e.target.data);
trace(myXML);
}
var mylvl:Number = myXML.main.@lvl;
var myhp:Number = myXML.main.@hp;
var mydmg:Number = myXML.main.@dmg;
var mydef:Number = myXML.main.@def;
var myexp:Number = myXML.main.@exp;
var mygold:Number = myXML.main.@gold;
lvlDis.text = String(mylvl);
hpDis.text = String(myhp);
dmgDis.text = String(mydmg);
defDis.text = String(mydef);
expDis.text = String(myexp);
goldDis.text = String(mygold);
“编译”选项卡上没有错误,但值不会显示
答案 0 :(得分:0)
你必须等到你的加载器完成加载xml文件然后你可以使用它的数据,所以你可以这样做:
var myLoader:URLLoader = new URLLoader();
myLoader.load(new URLRequest('db.xml'));
myLoader.addEventListener(Event.COMPLETE, processXML);
function processXML(e:Event):void
{
setTexts(new XML(e.target.data));
}
function setTexts(xml:XML):void
{
lvlDis.text = String(xml.main.@lvl);
hpDis.text = String(xml.main.@hp);
dmgDis.text = String(xml.main.@dmg);
defDis.text = String(xml.main.@def);
expDis.text = String(xml.main.@exp);
goldDis.text = String(xml.main.@gold);
}
希望可以提供帮助。