0

我正在尝试将高分数据加载到 SWF 中。我有 20 个动态文本字段。首字母 10 分,分数 10 分。

这是我的 SWF 中的 Actionscript:

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 文件在我桌面上的同一个文件夹中用于测试。

4

1 回答 1

1

对 ActionScript 代码中元素的引用不考虑 XML 结构的层次结构。

例如,name1andscore1是 的子级first,其引用为:

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;
}
于 2013-09-24T07:10:55.750 回答