Question

A website I'm working on (using AS2 because it's oldschool) has a larger index .swf file that loads sub-swfs using loadMovie("foo1.swf", placeToShowSwf). There's foo1.swf through 4, which is silly because the only thing that's different between them is a single number in the address of an xml file that tells it what content to load. So I want to reduce this to one file, with a simple function that the index file calls to load the xml file, as seen here.

function setFooNum(i:Number) {
   fooNum = i;
   //my_xml = new XML();  edit: this line has since been removed and is kept for historical purposes
   my_xml.load("foo"+fooNum+".xml");
};

However, for some reason, the xml file won't load. It loads properly outside the function, but that doesn't do me much good. It changes fooNum properly, but that doesn't do me any good if the wrong xml file is already loading. As far as I can tell, the code behaves as though the my_xml.load("foo"+fooNum+".xml") isn't there at all.

Is this some sort of security measure I don't know about, and is there any way around it?

EDIT As several people pointed out, the my_xml = new XML() line was the culprit. Unfortunately, I'm now getting a new and exciting error. When setFooNum(i) is called immediately after the loadMove() in the index file, a trace(fooNum) inside the setFooNum() function prints that fooNum is set correctly, but a trace(fooNum) inside the onLoad() (which returns a success despite loading apparently nothing, btw) shows that fooNum is undefined! Also, I made a button in the index swf that calls setFooNum(3) (for debugging purposes), which for some reason makes it work fine. So waiting a few seconds for the file to load seems to solve the problem, but that's an incredibly ugly solution.

So how do I wait until everything is completely loaded before calling setFooNum()?

Was it helpful?

Solution

Perhaps you should double-check that you have proper authorization in crossdomain.xml. If your crossdomain file is jacked, none of your requests will go through.

http://crossdomainxml.org/

OTHER TIPS

Your function is creating a new XML instance each time, and as a result you will need to define the onLoad function each time as well, if you reuse the same XML instance then you won't need to re-define the function, e.g.

var iFooNum:Number;
var oXML:XML = new XML();

oXML.onLoad = function (success:Boolean) {
  if(success) {
    //Do Something with XML.
  }
}

loadFooNum = function(i:Number) {
  iFooNum = i;
  oXML.load("foo" + i + ".xml");
}

If your loading the XML file from a different domain you will need a crossdomain.xml file on the other domain to allow flash to load the XML file.

Assuming you defined the onLoad event hanlder for your XMLinstance outside the function, you can remove the declaration of the instance from within the function body:

function setFooNum(i:Number) {
    fooNum = i;
    my_xml.load("foo"+fooNum+".xml");
};

This tutorial on kirupa website could help you.

http://www.kirupa.com/web/xml/index.htm

It explains how to work with AS and XML. Hope this helps you get the answer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top