A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: Images Loaded from XML Load in Wrong Order (On Stage)

  1. #1
    Senior Member
    Join Date
    Apr 2002
    Location
    Atlanta
    Posts
    103

    Images Loaded from XML Load in Wrong Order (On Stage)

    I can't understand it, phew...

    Will images load according to their size before XML order? I'm loading multiple images from XML like so:

    Code:
    XML...
    
    function formatXMLContent():void {
    	for (var i:int = 0; i < tourID.length(); i++) {			
    		var loader:Loader = new Loader();
    		loader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded);
    		loader.load(new URLRequest(tourImage[i]));		
    	
    		box = new Sprite();
    		box.graphics.beginFill(0x000000, 0);
    		box.graphics.drawRect(0, 0, 310, 340);
    		box.graphics.endFill();
    		box.x = i * (box.width - 45);
    		boxes.push(box);
    		content_mc.addChild(box);
    		
    		mapHotSpot = new MapHotSpot();
    		mapHotSpot.buttonMode = true;
    		mapHotSpot.useHandCursor = true;
    		mapHotSpot.x = 225;
    		mapHotSpot.y = 288;
    		MakeLink.Link(mapHotSpot, tourLink[i], "_blank");
    		box.addChild(mapHotSpot);
    	}
    }
    
    function imageLoaded(e:Event):void {
    	var image:Bitmap = (Bitmap)(e.target.content);
    	var holder = boxes[loadedImages];	
    	holder.addChild(image);
    	loadedImages++;
    }
    If I trace loadedImages or holder, etc... they trace in the RIGHT order.

    Why not on screen?

    Thank you for my sanity.
    Last edited by madkat; 11-05-2009 at 04:03 PM. Reason: Added to title

  2. #2
    rabid_Delineator AttackRabbit's Avatar
    Join Date
    Dec 2003
    Location
    Orlando, Florida
    Posts
    481
    probably because they are not loading in the order you want. so your imageLoaded mehtod is gonna get called when an image has finished loading. just because you are loading them in order , doesnt mean they are gonna finish order. Image one might take 2 seconds to load , while image two takes half a second , well image two will be added to the display list before image one so theyll be out of order.

  3. #3
    Senior Member
    Join Date
    Apr 2002
    Location
    Atlanta
    Posts
    103
    Ok that's what I thought AttackRabbit... so then is that where a preloader comes in or is it more something like the order of my code. Def. a newbie here with this. But will continue trying to figure it out...

  4. #4
    rabid_Delineator AttackRabbit's Avatar
    Join Date
    Dec 2003
    Location
    Orlando, Florida
    Posts
    481
    well just having one load complete handler , and not encapsulating this , you are gonna run into issues like this. A quick solution , albeit , not the best way to do this , would be to not load your image in the for loop. Do everything else , but remove the load part.
    so like

    Code:
    var loadImages = 0;
    
    function formatXMLContent():void {
    	for (var i:int = 0; i < tourID.length(); i++) {			
    		box = new Sprite();
    		box.graphics.beginFill(0x000000, 0);
    		box.graphics.drawRect(0, 0, 310, 340);
    		box.graphics.endFill();
    		box.x = i * (box.width - 45);
    		boxes.push(box);
    		content_mc.addChild(box);
    		
    		mapHotSpot = new MapHotSpot();
    		mapHotSpot.buttonMode = true;
    		mapHotSpot.useHandCursor = true;
    		mapHotSpot.x = 225;
    		mapHotSpot.y = 288;
    		MakeLink.Link(mapHotSpot, tourLink[i], "_blank");
    		box.addChild(mapHotSpot);
    	}
           addImages();
    }
    
    function addImages() : void {
    var loader:Loader = new Loader();
    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded);
    loader.load(new URLRequest(tourImage[loadedImages]));
    
    }
    
    function imageLoaded(e:Event):void {
    	var image:Bitmap = (Bitmap)(e.target.content);
    	var holder = boxes[loadedImages];	
    	holder.addChild(image);
    	loadedImages++;
    if( loadImages < tourID.length() ) addImages();
    }
    So basically , whats happening is that rather then using the for loop to add images , you set up everything else , then call addImages() , which will load the first image. Then once the first image has loaded , it will check to see if it needs to load another images , if so load that. this way , you are forcing the application to load the images in order

  5. #5
    Senior Member
    Join Date
    Apr 2002
    Location
    Atlanta
    Posts
    103

    resolved

    AttackRabbit, yep that works, thank you. I appreciate your explanation and understand the latter paragraph.

    I have to marinate on the rest but this is something I'm very interested in doing right. I know a lot of aspects of the code are inefficient --

    Appreciate your help again.

  6. #6
    rabid_Delineator AttackRabbit's Avatar
    Join Date
    Dec 2003
    Location
    Orlando, Florida
    Posts
    481
    np. Just keep in my mind , if the first image fails to load , they all will fail. So you might want to try to build in some error handling. you can tack on additonal listeners to your loaders , to listen for other events , in case a load fails , you can have it bypass that image and move onto the others.

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