AS3 - XML更改元素值不起作用?

时间:2013-09-14 12:53:17

标签: xml actionscript-3 flash air

我目前正在制作一个高分表,其中一个高分表是拼写测验。

我有一个名为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];

我不擅长编程,似乎无法弄明白。

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分: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]+">");