Jump to content
Search Community

Loads images in correct XML order (not filesize order)

kek test
Moderator Tag

Recommended Posts

Hello,

 

i load a xml file that contains :

 

<?xml version="1.0" encoding="iso-8859-1"?>






 

And when i load it, and then load the pictures in it (pictures are in "images/" folder), it loads first the smallest picture, to the heaviest.

I want it to load the pictures in the xml order (i.e : 1,2,3,4,5). Here is my code, i think i forgot something, but can't find...

import com.greensock.*;
import com.greensock.easing.*;
import com.greensock.events.LoaderEvent;
import com.greensock.loading.*;
import com.greensock.loading.display.*;

var images:Array = [];
var cont:MovieClip = new MovieClip();
addChild(cont);

var i:int;
var queue:LoaderMax = new LoaderMax();
LoaderMax.defaultAuditSize = false;

var xmlLoader:XMLLoader = new XMLLoader("images.xml",{onComplete:onLoadXML});

xmlLoader.load();

function onLoadXML(e:LoaderEvent):void {
var xmlData:XML = e.target.content;

try {
	for each (var element:XML in xmlData.elements()) {
		var imageData:Object = new Object();
		imageData.url = "images/" + element. @url.toString();
		imageData.loader = new ImageLoader(imageData.url,{width:307,height:285,scaleMode:"proportionalInside",alpha:0});
		images.push(imageData);
		queue.append(imageData.loader);
	}

	var maxImages:int = images.length;

	for(i=0;i			images[i].loader.addEventListener(LoaderEvent.COMPLETE, startApplication);
	}

	queue.load();

} catch (err:Error) {
	trace("Error code: " + err.message);
	return;
}
}

function startApplication(e:LoaderEvent):void {
var img:* = e.target.content;
trace(img.loader.url);
//TRACE images/im2.jpg - images/im1.jpg - images/im4.jpg - images/im3.jpg - images/im5.jpg

img.x = cont.width;
cont.addChild(img);

TweenLite.to(e.target.content, 1, {alpha:1});
}

 

(BTW, i tried to use prependURLs but can't manage it..)

Link to comment
Share on other sites

Keep in mind that by default, a LoaderMax's maxConnections is 2 which means it will load 2 files at a time. As soon as it finishes one, it moves on to the next one in the queue. So it sounds like you're getting expected behavior. Imagine image 2 is much smaller than image 1 - both will start loading initially and since 2 is smaller, it will finish first. Then image 3 begins loading. Then while 3 is loading, maybe 2 finishes so LoaderMax moves on to image 4. If that image is much smaller than 3, it would finish first, etc.

 

See why?

 

The solution is simple - set the LoaderMax's maxConnections to 1.

 

var queue:LoaderMax = new LoaderMax({maxConnections:1});

 

As far as using prependURLs(), it should be as simple as dumping all your ImageLoaders in there and then calling

queue.prependURLs("images/") before you load() the queue.

 

Of course you should always make sure you're using the latest version too. http://www.greensock.com/loadermax/

 

Does that solve things for you?

Link to comment
Share on other sites

Oh thank you !

In fact I thought LoadeMax loaded the smallest files first, to the heaviest, by default. But i forgot the number of simultaneous downloads :)

So everything is normal in fact.. It's just that i want files appear in the correct order, so i'll try something else.

 

Thanks again !

Link to comment
Share on other sites

Yes, i'll try that, but is it slower than setting maxConnections to 2 ?

i mean, if i load 100 images, 1 by 1, it's slower than 2 by 2, no ?

 

In fact, i'll try to load with maxConnections to 2, and when loaded, put the files in an array in correct order, and then show array[0], array[1] etc...

Link to comment
Share on other sites

Yes, it is definitely slower to load things 1 at a time like that. That's why the default maxConnections is 2.

 

You probably don't need to reorder things manually like that - why not just do something like this?:

 

var myImages:Array = queue.content;

 

A LoaderMax's "content" is an array of all of its loaders content, in the correct order according to the queue positions.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...