0

所以目前我能够根据用户的需要保存 1-9 个 AS3 变量,它们作为文本字符串保存到 XML 文件中,但是当用户再次保存时,旧的 xml 信息仍保留在文件中并添加了新信息。但是,我希望新信息要么覆盖旧信息,要么设置一个函数以在保存新设置之前清除/删除 xml 文件中的信息。

这是我当前的保存功能:

var soundVAR;
var conditionVAR;
var soundtoXML:XML; 
var conditiontoXML:XML; 
var test = 1;
var playingCounter = 0;
var xml:XML = <saved>
        </saved>;


//1.Button event listener
btn_Save.addEventListener(MouseEvent.CLICK, saveXML);
//2.The saveurl function which opens a URL.
function saveXML(event:MouseEvent):void {


var conditionsArray = new Array(play1Condition, play2Condition, play3Condition, play4Condition, play5Condition, play6Condition, play7Condition, play8Condition, play9Condition)

for (var i=0; i < conditionsArray.length; i++){
trace(conditionsArray[i])
if(conditionsArray[i] == true)
{
    soundVAR = soundsName[i];
    conditionVAR = conditionsArray[i];


    playingCounter++;    
} else {

    trace('no sounds are playing');
}

while (xml.item.length() < playingCounter ){
    var soundString:String =  '<sound>' + soundVAR + '</sound>';
    var conditionString:String = '<condition>' + conditionVAR + '</condition>';
    soundtoXML = new XML(soundString);
    conditiontoXML = new XML(conditionString);

    var item:XML = <item />
    item.@id = xml.item.length().toString();
    xml.appendChild(item);

    var sound:XML = xml.item[xml.item.length() - 1];
    var condition:XML = xml.item[xml.item.length() - 1];
    sound.appendChild(soundtoXML);
    condition.appendChild(conditiontoXML);
}
}
writeXML();
trace(xml);
}

function writeXML(){

var savedSounds:SharedObject = SharedObject.getLocal( 'savedSounds');
savedSounds.data.soundsxml = xml; // your variables
savedSounds.flush(); // stores data into disk


}

提前致谢。

4

1 回答 1

0

简短的回答:你想在循环xml.item.setChildern('');之前调用。while更长的答案:我不明白 while 循环的最后 4 行。你能解释一下写它们的意图是什么吗?

如果我正确理解了您的代码,那么 while 循环可能如下所示:

var i:int = xml.item.length();
while (i < playingCounter)
{
    xml.* += <item id={ i }>
          <sound>{ soundVAR }</sound>
      <condition>{ conditionVAR }</condition>
    </item>;
    i++;
}
于 2012-06-02T13:17:16.777 回答