-
[AS2] new XML() in Loop Error
Hi there,
(Code: AS2)
I've written the below script. Basically, I set out wanting to make a reusable piece of code that would take the names of some xml files (as listed in the cats array), and to cycle through each file, loading the XML data into the kitties object. Then, once the info had loaded, it goes through the cat nodes, and creates a new Cat object from them.
This works great if I just use one file i.e. just new Array("moggies.xml").
But, if I use more than one, the code whizzes through, ignores the onLoad section and only bothers to apply the load and onLoad to the final file in the array. It's as if doesn't wait for the XML to load until the last moment. The XML files are all valid and work fine.
The below code outputs:
Currently on: persians.xml
Currently on: moggies.xml
Currently inside: moggies.xml
Currently inside: moggies.xml
Created new moggies called moggies0 with the filename flump.jpg
Created new moggies called moggies1 with the filename bozo.jpg
Created new moggies called moggies2 with the filename mogz.jpg
Created new moggies called moggies3 with the filename missy.jpg
Any help would be much appreciated.
Code:
var cats:Array=new Array("persians.xml","moggies.xml");
for(var i:Number=0; i<cats.length; i++)
{
var file:String=cats[i];
var kitties:XML=new XML();
kitties.ignoreWhite=true;
trace("Currently on: "+file);
kitties.onLoad=function(success:Boolean)
{
trace("Currently inside: "+file);
if(success)
{
var kitties_size:Number=kitties.firstChild.childNodes.length;
var type:String=kitties.firstChild.nodeName;
for(var j:Number=0;j<kitties_size;j++)
{
var nname:String=type+j;
var fname:String=kitties.firstChild.childNodes[j].attributes.filename;
var iname:String=kitties.firstChild.childNodes[j].attributes.name;
var nname:Object=new Cat(type,nname,iname,fname);
}
}
}
kitties.load(file);
}
As an example of the moggies.xml sheet, it looks like this:
Code:
<?xml version="1.0" encoding="iso-8859-1"?>
<moggies>
<image name="Flump" filename="flump.jpg" />
<image name="Bozo" filename="bozo.jpg" />
<image name="Mogz" filename="mogz.jpg" />
<image name="Missy" filename="missy.jpg" />
</moggies>
Last edited by Solange; 04-26-2007 at 10:37 AM.
He took daddy's umbrella!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|