Dynamically changing the referenced XML file in Flash AS3?

拈花ヽ惹草 提交于 2020-01-15 11:58:26

问题


Basically I have media being parsed and played by reference of an XML document using AS3. These media files are gonna be seperated into seperate XML files then what I was planning was to just dynamically change which XML file is being referenced. Problem is, the code only fires once on frame 2 and I can't seem to figure out how to switch out the XML file for a different one upon an event trigger. "new URLRequest(www.blah.com/theotherXMLfile.xml)" isn't working...do I need to reload a whole other string of code to change to a different XML document?

I'll post the code if you need it...


回答1:


Not sure if this is what you were asking, but to update the XML, you'll need the URLLoader to reload the file and then reparse the XML document.

edit

Here is a quick example on how to reuse the URLRequest. I hope I didn't put any syntax errors in it, had a longer AS break.. :P

private var dataUrl:URLRequest;
private var xmlLoader:XMLLoader;
private var xmlDoc:XML;

private function init ():void
{
    dataUrl = new URLRequest( ... );

    xmlLoader = new XMLLoader( dataUrl );
    xmlLoader.addEventListener( Event.COMPLETE, parseXML );
}

public function reloadXML ()
{
    xmlLoader.load( dataUrl );
}

private function parseXML ( event:Event )
{
    xmlDoc = new XML( xmlLoader.text );

    // do something else, for example update UI etc.
}



回答2:


You could load a new XML with the same URLLoader

var xmlLoader:URLLoader = new URLLoader();
xmlLoader.addEventListener(Event.COMPLETE , completeHandler );
// configure your other listeners here for IOError etc...
var request:URLRequest;

function loadXML(url:String ):void
{
    request = new URLRequest(url);
    xmlLoader.load(request );
}




来源:https://stackoverflow.com/questions/3817429/dynamically-changing-the-referenced-xml-file-in-flash-as3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!