views:

53

answers:

2

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

+2  A: 

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.
}
poke
Yep, one way or the other, you're going to have to load each xml file.
UltimateBrent
Thanks for such a quick response! Can I recycle the URLLoader? i.e. XMLLoaderThing:URLLoader = new URL.....just with a different URL to the new/next XML file or do I have to give each XML file it's own loader with it's own name? That's where I'm having an issue. When I trigger a "new URLLoader" with the same name, i.e.XMLLoaderThing (haven't tried different names), it's returning null or giving the "No property" error.
Aaron
You can just call the existing URLLoader's `load` method with the new URL. That way the URLLoader will recycle everything, even event handlers in which you might parse the XML from the loaded data.
poke
The way I have my functions nested with all the if statements, even using this style kept causing a (null property) error upon compiling. I found a very fast and efficient resolution below (works perfect with AIR for Android!).
Aaron
+1  A: 

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


PatrickS
You should add the listeners only once.
poke
They're only added once. Look again... I just added one for the complete event and commented that other listeners should go where the comment is, namely before calling the load method
PatrickS
Because I couldn't get it to work (nesting issues) II have an idea. I was thinking of making a separate swf for each xml section...for example, xml1's control and content on xml1.swf, xml2 content and controls in xml2.swf, etc. and loading them externally so that when I trigger an event listener, it will simply unload the old swf and load the new. Should work right?
Aaron
well yes it would work... but... in that case it'd be more efficient for each swf to call its own xml , i mean it wouldn't be necessary to have a central xml in your main swf. is that what you had in mind?... it's not the most flexible solution... it sort of defeats the purpose of xml , you may as well hardcode the value if you're taking that direction. one of the main advantage in using xml is the ability to make changes to your swf without recompiling... and you're basically loosing that. i'm not even mentioning the loading process for each swf.
PatrickS
Externalizing all that had to do with the xml file and leaving the listeners in the main swf works flawlessly! I basically strung each swf to it's own xml file and just loaded the swf externally accordingly...Thanks for all the help guys! This site teaches me something new everyday :)
Aaron
@PatrickS: What I meant is that when you call the `loadXML` function multiple times, which you do, when you want to reload it (?), then you are adding the listeners again.
poke
@poke ... you're right! ;)
PatrickS