XML and loadMovie
A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: XML and loadMovie

  1. #1
    Junior Member
    Join Date
    May 2001
    Posts
    3

    Wink

    I have created a SWF which loads an XML file and calls a function on the onload event, which in turn calls another function. This works fine. I then load this SWF into a parent SWF into Level 1 (or higher) or a movie clip. It no longer works (it does work on level 0) because the 2nd function can not be accessed.

    I have reduced the code to a quick example:

    The “child” SWF: (action script in frame 1)

    XMLData = new XML();
    XMLData.onLoad = function1;
    XMLData.load("SimpleXMLData.xml");

    function function1() {
    trace("Inside function 1")
    num = 4
    trace("Inside function 1 before call : num - " + num)
    num = function2(num)
    trace("Inside function 1 after call : num - " + num)
    }

    function function2(num) {
    trace("Inside function 2: num - " + num)
    return num + 1
    }


    The “parent” SWF: (action script in frame 1)

    loadMovieNum ("Child.swf", 1);
    or loadMovie ("Child.swf", "_root.EmptyMC"); both fail, but

    loadMovieNum ("Child.swf", 0); works

    I don't think the XML file matters, but here it is anyway:

    SimpleXMLData.xml
    <news>
    <article>Space Shuttle lifts off</article>
    <article>Flash is the best</article>
    </news>

    Any ideas would be appreciated. I hope I am just missing something, but maybe this just isn't doable.

    Thanks

  2. #2
    Senior Member
    Join Date
    Feb 2001
    Location
    Provo, Utah
    Posts
    1,112

    Uhh...

    It's definently not a problem with the XML object. That does it's job fine. You've got some other problems.. which have caught my attention - since I can't figure out why it won't work... so - I'm gonna look into it, and I'll try to get back to you if I find an answer.

  3. #3
    Junior Member
    Join Date
    May 2001
    Posts
    3

    Wink

    Thank you for looking at this.

    I know the XML object is loading the XML file correctly, but the reason I mention the XML object is that if I replace all the XML code

    ------
    XMLData = new XML();
    XMLData.onLoad = function1;
    XMLData.load("SimpleXMLData.xml");
    ------

    with a call to function1 directly like:

    temp = function1();

    everything works fine everywhere. That's when I assumed the XML object had something to do with it. But again, I don't know right now.

    Thanks again

  4. #4
    Senior Member
    Join Date
    Oct 2000
    Posts
    157
    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.

    Code:
    XMLData.onLoad = function1;
    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'.

    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.
    Code:
    _root.XMLData = new XML(); 
    _root.XMLData.onLoad = function1; 
    _root.XMLData.load("SimpleXMLData.xml");
    I haven't tried this yet, but the function should then run on data off of '_root' where the function is being run.

    alternately surrounding your code in a with() statement should do the same.

    Code:
    with ("_root") {
       XMLData = new XML(); 
       XMLData.onLoad = function1; 
       XMLData.load("SimpleXMLData.xml"); 
    }
    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.

    if the swf is loaded into '_root.emptyMC.' then:
    Code:
    XMLData.onLoad = function1; 
    
    function function1() { 
       with ("_root.emptyMC") {
          trace("parsing XML");
       }
    }
    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.
    When the flag is set, then run the parsing code desired.

    Code:
    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");
    }
    - Tony

  5. #5
    Senior Member tupps's Avatar
    Join Date
    Jan 2001
    Location
    Melbourne
    Posts
    2,035
    I have also seen some problems when you load flash movies into levels other than level 0. It seems that when passing some information from different parts of the movie via _root no longer works. For my simple flash file changing the references from _root to _level5 worked fine but had nothing to do with XML.

    I have a feeling that there are some general issues with using different levels that need to be investigated. From my understanding _root and _level5 when loaded into level 5 should result in the same thing (root is relative to the level that you have loaded something in), however this is obviously not the case.

    Thanks

    Luke

  6. #6
    Senior Member
    Join Date
    Oct 2000
    Posts
    157
    OK, you're right. We should pass things by '_level0.' I guess.
    '_level0.' is an absolute location.

    - Tony

  7. #7
    Senior Member
    Join Date
    Oct 2000
    Posts
    157
    ARGGHHH!!!!

    OK, as a final note. The problem described seems to be fixed in the Flash 5.0.42.0 plugin. So using the 'with()' statement in the function called by XMLdoc.onload no longer works in the 42 plugin release.

    So now I doublely recommend just using the XMLdoc.onload event handler to set a flag off of '_level0' and testing for that to parse the XML rather than parsing it in the function itself.

    - Tony

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width


HTML5 Development Center

Poll by Flashkit.com