PDA

View Full Version : Loader question



ookla
December 1st, 2008, 03:27 PM
Hi there....

I put this loop inside another loop..

for(ii; ii<l; ii++){
ld = new Loader();
ld.x = Math.random()*740;
ld.y = Math.random()*216;
_container.addChild(ld);
rq = new URLRequest(xmll.children()[ii]);
ld.load(rq);
}

So, my question is.. everytime my this loads, a new loader is created right? so, everytime it downloads my pictures from my webserver? I mean, after some hours, this script downloads the same picture over and over? I have to say goodbye to my bandwidth?

I appreaciate any help..

theCodeBot
December 1st, 2008, 04:15 PM
In a way, this could be what you want. If you don't want the user's memory to be chewed up by a bunch of images when you should only have a couple loaded at a time, then this is what you're stuck doing. If, however, there aren't enough images for the user to have to worry about memory, then there's hope.

You can store loaded images to an array of loaders, and when you go to load one, check if it's already been loaded and act accordingly:


var alreadyLoaded:Object = {};
//Gets declared before you start looping.
//And in your internal for loop with ii:
for(var ii:int=0;ii<l;ii++) {
var ld:Loader;
if(alreadyLoaded[xmll.children()[ii]]) {
ld=alreadyLoaded[xmll.children()[ii]];
//If we already loaded it, use the loaded one.
}
else {
//Otherwise, load it and store it.
var rq:URLRequest = new URLRequest(xmll.children()[ii]);
ld.load(rq);
}
//Now work with it.
ld.x = Math.random()*740;
ld.y = Math.random()*216;
_container.addChild(ld);
}