2013-09-24 113 views
0

我試圖將高分數據加載到SWF文件中。我有20個動態文本字段。 10爲首字母,10爲分數。將XML數據加載到SWF文件時出現問題

這是我的Actionscript在我的SWF:

var xmlData:XML = new XML(); 
var theURL_ur:URLRequest = new URLRequest("joust_test.xml"); 
var loader_ul:URLLoader = new URLLoader(theURL_ur); 
loader_ul.addEventListener("complete", fileLoaded); 

function fileLoaded(e:Event):void 
{ 
    xmlData = XML(loader_ul.data); 

    n1_txt.text = xmlData.name1; 
    n2_txt.text = xmlData.name2; 
    n3_txt.text = xmlData.name3; 
    n4_txt.text = xmlData.name4; 
    n5_txt.text = xmlData.name5; 
    n6_txt.text = xmlData.name6; 
    n7_txt.text = xmlData.name7; 
    n8_txt.text = xmlData.name8; 
    n9_txt.text = xmlData.name9; 
    n10_txt.text = xmlData.name10; 
    s1_txt.text = xmlData.score1; 
    s2_txt.text = xmlData.score2; 
    s3_txt.text = xmlData.score3; 
    s4_txt.text = xmlData.score4; 
    s5_txt.text = xmlData.score5; 
    s6_txt.text = xmlData.score6; 
    s7_txt.text = xmlData.score7; 
    s8_txt.text = xmlData.score8; 
    s9_txt.text = xmlData.score9; 
    s10_txt.text = xmlData.score1; 

} 

這裏是我的XML文件:

<?xml version="1.0" encoding="utf-8"?> 
<joust> 
    <first> 
     <name1>BBH</name1> 
     <score1>515250</score1> 
    </first> 
    <second> 
     <name2>BBH</name2> 
     <score2>250600</score2> 
    </second> 
    <third> 
     <name3>MRM</name3> 
     <score3>239050</score3> 
    </third> 
    <fourth> 
     <name4>n/a</name4> 
     <score4>229800</score4> 
    </fourth> 
    <fifth> 
     <name5>REI</name5> 
     <score5>215500</score5> 
    </fifth> 
    <sixth> 
     <name6>CTS</name6> 
     <score6>177650</score6> 
    </sixth> 
    <seventh> 
     <name7>JTK</name7> 
     <score7>175150</score7> 
    </seventh> 
    <eighth> 
     <name8>JTK</name8> 
     <score8>173900</score8> 
    </eighth> 
    <ninth> 
     <name9>ACH</name9> 
     <score9>171900</score9> 
    </ninth> 
    <tenth> 
     <name10>ZAQ</name10> 
     <score10>153110</score10> 
    </tenth> 
</joust> 

有人可以幫助我弄清楚什麼我可以做錯了什麼? SWF和XML文件位於桌面上的相同文件夾中進行測試。

回答

1

對ActionScript代碼中元素的引用不會考慮XML結構的層次結構。

例如,name1score1first一個孩子,這將被引用爲:

xmlData.first.name1 
xmlData.first.score1 

或者,你可以使用後代存取器(..)運算符可以訪問XML對象的子屬性:

xmlData..name1 
xmlData..score1 

所以,你的代碼將如下所示:

var loader:URLLoader = new URLLoader(new URLRequest("joust_test.xml")); 
loader.addEventListener(Event.COMPLETE, loaderCompleteHandler); 

function loaderCompleteHandler(event:Event):void 
{ 
    var xml:XML = new XML(event.target.data); 

    n1_txt.text = xml.first.name1; 
    s1_txt.text = xml.first.score1; 

    /* or, you can use descendant accessor */ 

    n2_txt.text = xml..name2; 
    s2_txt.text = xml..score2; 
} 
相關問題