0

我目前正在制作一张高分表,其中一张高分表用于拼写测验。

我有一个名为 的本地文件highscores.xml,如下所示:

<highscores>
    <spelling>
        <first>0</first>
        <second>0</second>
        <third>0</third>
        <fourth>0</fourth>
        <fifth>0</fifth>
    </spelling>
</highscores>

这是加载highscores.xml文件后运行的 AS3 函数:

function highScoresLoaded(e:Event)
{
    highScoresXML = new XML(urlLoader.data);
    highScoresArray = new Array();
    highScoresArray.push(highScoresXML.spelling.first);
    highScoresArray.push(highScoresXML.spelling.second);
    highScoresArray.push(highScoresXML.spelling.third);
    highScoresArray.push(highScoresXML.spelling.fourth);
    highScoresArray.push(highScoresXML.spelling.fifth);
    highScoresArray.push(points);
    highScoresArray = highScoresArray.sort(Array.DESCENDING);
    highScoresArray.pop();

    firstScoreTXT.text = highScoresArray[0];
    secondScoreTXT.text = highScoresArray[1];
    thirdScoreTXT.text = highScoresArray[2];
    fourthScoreTXT.text = highScoresArray[3];
    fifthScoreTXT.text = highScoresArray[4];

    highScoresXML.spelling.first = highScoresArray[0];
    highScoresXML.spelling.second = highScoresArray[1];
    highScoresXML.spelling.third = highScoresArray[2];
    highScoresXML.spelling.fourth = highScoresArray[3];
    highScoresXML.spelling.fifth = highScoresArray[4];

    trace(highScoresXML);
}

但是函数末尾的跟踪输出是这样的(我刚刚在测验中得了 4250):

<highscores>
  <spelling>
    <first>4250</first>
    <second>0</second>
    <third>0</third>
    <first>4250</first>
    <fifth>0</fifth>
  </spelling>
</highscores>

当我删除这一行时,它工作正常:

highScoresXML.spelling.fourth = highScoresArray[3];

我不擅长编程,似乎无法弄清楚。

任何帮助,将不胜感激!

4

1 回答 1

1

也许这就是你需要的:

var highScoresXML:XML =<highscores>
    <spelling>
        <first>0</first>
        <second>0</second>
        <third>0</third>
        <fourth>0</fourth>
        <fifth>0</fifth>
    </spelling>
</highscores>;


var highScoresArray:Array = new Array();
var highScoresNamesArray:Array = new Array();

for (var i:uint = 0; i<highScoresXML.spelling.children().length(); i++){
    highScoresArray.push(highScoresXML.spelling.children()[i]);
    highScoresNamesArray.push(highScoresXML.spelling.children()[i].name() );
}

highScoresArray.push(100);
highScoresArray = highScoresArray.sort(Array.DESCENDING);
highScoresArray.pop();

highScoresXML.spelling = new XML();

for(i=0; i<highScoresNamesArray.length;i++)
    highScoresXML.spelling.appendChild("<"+highScoresNamesArray[i]+">"
                                                  +highScoresArray[i]
                                          +"</"+highScoresNamesArray[i]+">");
于 2013-09-14T14:05:49.173 回答