yves
yves

Reputation: 29

LoaderMax: setting array as a container (ImageLoader)

So, I have a LoaderMax instance loading images from various URLs. I want to add all loaded images to an array. Here's my code:

var photosArray:Array = new Array(5);
var imageLoadingQueue:LoaderMax = new LoaderMax({name:"mainQueue", onComplete:completeHandler});

for (var g:uint=0; g<5; g++)
{
    imageLoadingQueue.append(new ImageLoader("/img" + g + ".jpg", {name:"photo", container:photosArray[g], noCache:false, smoothing:true, width:126, height:126, scaleMode:"proportionalOutside"}));
} 

imageLoadingQueue.load();

private function completeHandler(e:LoaderEvent):void
{
    trace("finished loading pictures!");

  //the next two lines will return an error (saying that photosArray[1] is null)
    stage.addChild(photosArray[1]);
    photosArray[1].x = 250;
}

A few problems:

  1. If I set the container of the image being loaded to the Array, it won't work. I'm not being able to access the image inside the array because it says it's null.
  2. If I set the container of the image being loaded to "this" (using the container property when appending a new ImageLoader) and, on the completeHandler, set my array equal to event.target.content, it kinda works (but it's not the ideal). The problem is that, by doing so, the images are appearing on the stage as they are loaded, and I do no want them to do so.

Any help would be heavily appreciated. Thanks!!

Upvotes: 0

Views: 396

Answers (2)

Jack
Jack

Reputation: 2970

David is correct, but I also wanted to mention that the LoaderMax's "content" is actually an array of all of its children's content, so you could just use that for simplicity. Keep in mind that ImageLoaders automatically create a Sprite (technically called a "ContentDisplay") to drop the image into so you probably don't need to create ANOTHER Sprite (a container for the container).

var photos:Array = imageLoadingQueue.content;
stage.addChild(photos[1]);

The other nice thing is that it creates the ContentDisplay Sprites immediately, even before any content is loaded into them, so you can place them and size them however you want while (or before or after) loading occurs.

Upvotes: 2

David Mear
David Mear

Reputation: 2254

The container needs to be a DisplayObjectContainer. ImageLoader will try to add the image to the container using addChild(), so obviously this won't work with an empty array. Create a new Sprite for each image and add it into the array first:

for (var g:uint=0; g<5; g++)
{
    photosArray[g] = new Sprite();
    imageLoadingQueue.append(new ImageLoader("/img" + g + ".jpg", {name:"photo", container:photosArray[g], noCache:false, smoothing:true, width:126, height:126, scaleMode:"proportionalOutside"}));
}

Upvotes: 1

Related Questions