This content has been marked as final. Show 3 replies
> But when I go to trace the .xml file with this:
> I get nothing.
This looks like a matter of timing. Your first five lines are
encountered one after the other, starting with var myXML through
myLoader.addEventListener(), which calls a function defined immediately
below that. The function is called when the "complete" event is dispatched,
which may take longer or shorter depending on the size of the XML document
and network traffic. If you put a trace() statement immediately below that,
Flash will execute that too -- and at the moment it does, your XML document
hasn't loaded yet.
> However, if I place this trace inside the xmlLoaded function it
> traces it out. My problem is I want access to the info outside
> of that function. What am I doing wrong?
I copy/pasted your code exactly, then followed it with this:
var t:Timer = new Timer(5000);
... which waits five seconds, then runs that trace(). Sure enough, it
outputs the XML contents.
If you want to use the myXML variable elsewhere in your movie, you just
have to make sure those other locations don't reference the variable until
it has a value (or account for a lack of value in the logic they use). You
might, for example, put a stop() action in frame one, along with the
existing code (and instead of my timer), then have the "complete" event
handler issue a play() action. In later frames, the myXML variable would
have the data you need, because the playhead won't advance until the data
have been loaded.
Co-author, Foundation Flash CS3 for Designers
"Luck is the residue of good design."
Perfect! Thank you very much David for the info. What I ended up doing what you suggested, putting a gotoAndStop() event after the info had loaded and then just started the rest of the movie a few frames down. Works great. Thanks again.
On a similar topic:
When an XML is being loaded from an imported class, timing also plays a role. The event listener should not be placed from the URLLoader-type property of the custom class, but rather the instance of the class.
var instanceXML = new LoadXML(); //where LoadXML is a custom class that does the URL request and XML load.
var instanceXML .loader.addEventListener( Event.COMPLETE, XMLLoadComplete ); //where "loader" is a property type URLLoader within the LoadXML class.
Instead of adding the event listener on the loader property within the LoadXML class (as many tutorials would have you do), the event listener should be placed on the loader property of the instance. Parsing a lengthy XML would cause empty variables to be passed before the load has terminated. Hope this is clear and that it helps someone as it had me stuck for a few hours.
Thanks David for the clue.