I just ran into this problem yesterday. This is what I think is happening. If anyone has a better explanation, or if anyone can confirm, or just has better terminology please post.
I think what happens is normally when a function is defined or an object is created it is relative to the timeline it was defined at. You'll see that XMLData will be created within the timeline the code was run at. '_root.' if the swf is run standalone, or '_level1.' or '_root.EmptyMC' if the swf is loaded into a level or movieClip.
This statement is different however.
As near as I can tell 'function1' populates an existing empty function of the XML object and as such it is off the '_root' timeline of '_level0'. If the swf is loaded into a higher level than 0, or if it is loaded into a movieClip the data will be where expected, but the function will unexpectedly be running relative to '_root'.Code:XMLData.onLoad = function1;
I don't know if this is a bug, or an as designed feature of object oriented programming. I haven't seen any other structure that behaves that way, although I'm very new to XML and newer features of Flash.
I see two workarounds.
1) Create your data off the root timeline.
I haven't tried this yet, but the function should then run on data off of '_root' where the function is being run.Code:_root.XMLData = new XML(); _root.XMLData.onLoad = function1; _root.XMLData.load("SimpleXMLData.xml");
alternately surrounding your code in a with() statement should do the same.
the code I tried first and have stuck with for the moment surrounds the code in the function with a 'with()' statement, explicitly telling that the commands should be run from where the data is.Code:with ("_root") { XMLData = new XML(); XMLData.onLoad = function1; XMLData.load("SimpleXMLData.xml"); }
if the swf is loaded into '_root.emptyMC.' then:
this worked, but I find it inelegant. The swf will no longer work standalone, and the loaded MC must be explicitly stated in a different file from which it is set. The solution I'm leaning toward now is to simply have the .onload function set a flag on the _root timeline and do a looping check for the flag within the code of the swf.Code:XMLData.onLoad = function1; function function1() { with ("_root.emptyMC") { trace("parsing XML"); } }
When the flag is set, then run the parsing code desired.
- TonyCode:XMLData.onLoad = function1; function function1() { // set flag _root.XMLLoaded = 1; } function parseXML() { //parse the XML data when flag is set } //frame loop code if (_root.XMLLoaded == 1) { parseXML(); } else { gotoAndPlay ("loop"); }




Reply With Quote