A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: [AS2] new XML() in Loop Error

  1. #1
    Member
    Join Date
    May 2004
    Posts
    57

    [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!

  2. #2
    Senior Member
    Join Date
    May 2004
    Posts
    226
    since your XML object is initalized in your for loop, at every iteration you are re-initializing it before it had a chance to load.

    change kitties to an array defined outside your loop. Then push anonymous XML objects into. Try something like the following (untested):

    Code:
    var cats:Array=new Array("persians.xml","moggies.xml");
    var kitties:Array = new Array();
    
    for(var i:Number=0; i<cats.length; i++)
    {
    	var file:String=cats[i];
    
    	kitties.push( new XML() );
    	var myKitty = kitties[ i ];
    
    	myKitty.ignoreWhite=true;
    	trace("Currently on: "+file);
    	myKitty.onLoad=function(success:Boolean)
    	{
    		trace("Currently inside: "+file);
    		if(success)
    		{
    			var kitties_size:Number=kitties.firstChild.childNodes.  length;
    			var type:String=this.firstChild.nodeName;
    			for(var j:Number=0;j<kitties_size;j++)
    			{
    				var nname:String=type+j;
    				var fname:String=this.firstChild.childNodes[j].attributes.filename;
    				var iname:String=this.firstChild.childNodes[j].attributes.name;
    				var nname:Object=new Cat(type,nname,iname,fname);
    			}
    		}
    	}
    	myKitty.load(file);
    }

  3. #3
    Member
    Join Date
    May 2004
    Posts
    57
    Thanks for responding - will tinker around and give it a go
    He took daddy's umbrella!

  4. #4
    Member
    Join Date
    May 2004
    Posts
    57
    Quote Originally Posted by v5000
    since your XML object is initalized in your for loop, at every iteration you are re-initializing it before it had a chance to load.

    change kitties to an array defined outside your loop. Then push anonymous XML objects into. Try something like the following (untested):

    Code:
    var cats:Array=new Array("persians.xml","moggies.xml");
    var kitties:Array = new Array();
    
    for(var i:Number=0; i<cats.length; i++)
    {
    	var file:String=cats[i];
    
    	kitties.push( new XML() );
    	var myKitty = kitties[ i ];
    
    	myKitty.ignoreWhite=true;
    	trace("Currently on: "+file);
    	myKitty.onLoad=function(success:Boolean)
    	{
    		trace("Currently inside: "+file);
    		if(success)
    		{
    			var kitties_size:Number=kitties.firstChild.childNodes.  length;
    			var type:String=this.firstChild.nodeName;
    			for(var j:Number=0;j<kitties_size;j++)
    			{
    				var nname:String=type+j;
    				var fname:String=this.firstChild.childNodes[j].attributes.filename;
    				var iname:String=this.firstChild.childNodes[j].attributes.name;
    				var nname:Object=new Cat(type,nname,iname,fname);
    			}
    		}
    	}
    	myKitty.load(file);
    }
    Superb. Works :-) There's a teeny weeny typo in the above code, though, in case an outsider copies and pastes the code without reading it:

    var kitties_size:Number=kitties.firstChild.childNodes. length;
    should be: var kitties_size:Number=this.firstChild.childNodes.len gth;

    Thanks again
    He took daddy's umbrella!

  5. #5
    Senior Member
    Join Date
    May 2004
    Posts
    226
    Glad it worked for you. Also, another thing you might want to do is make a counter so you know all your xml files have been loaded and parsed before exiting the frame. It might save you some headaches later, something like this:

    var cats:Array=new Array("persians.xml","moggies.xml");
    var kitties:Array = new Array();
    var counter = 0;
    stop();

    //then in your onload function put
    counter++;
    if( counter == cats.length ) play();

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