简体   繁体   中英

Dynamically changing the referenced XML file in Flash AS3?

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...

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.
}

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 );
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM