views:

1985

answers:

4

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()?

A: 

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.

Ady
onLoad is defined elsewhere. my_xml.load works when it's not called in a function, but I need to change what xml file is loaded, and then load it.
mgbennet
but you are creating a new instance in your function, and as a result onLoad is not defined.
Ady
You're right. That was silly of me. But now I get a different error. If I call setFooNum(i) immediately after the loadMovie(), the swf gets one of those "A script is causing your computer to run slowly. Cancel?" errors, which I assume means infinite loop or crash. Anyways, updated my question.
mgbennet
A: 

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");
};
Claudio
A: 

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.

Ryuken
A: 

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/

mattbasta