LoaderMax:将数组设置为容器(ImageLoader)

时间:2013-04-09 13:57:35

标签: arrays image actionscript-3 greensock

所以,我有一个LoaderMax实例从各种URL加载图像。我想将所有加载的图像添加到数组中。 这是我的代码:

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;
}

一些问题:

  1. 如果我将正在加载的图像的容器设置为数组,则它将无法工作。我无法访问数组中的图像,因为它表示它是空的。
  2. 如果我将正在加载的图像的容器设置为“this”(在附加新的ImageLoader时使用容器属性),并且在completeHandler上,将我的数组设置为等于event.target.content,它有点工作(但是这不是理想的)。问题在于,通过这样做,图像在加载时出现在舞台上,我不希望它们这样做。
  3. 任何帮助都会受到高度赞赏。 谢谢!

2 个答案:

答案 0 :(得分:2)

大卫是正确的,但我也想提一下,LoaderMax的“内容”实际上是一个包含所有孩子内容的数组,所以你可以简单地使用它。请记住,ImageLoaders会自动创建一个Sprite(技术上称为“ContentDisplay”)来删除图像,因此您可能不需要创建另一个Sprite(容器的容器)。

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

另一个好处是它会立即创建ContentDisplay Sprites,甚至在任何内容加载到它们之前,因此您可以在加载发生时(或之前或之后)随意放置它们并调整它们的大小。

答案 1 :(得分:1)

容器必须是DisplayObjectContainerImageLoader将尝试使用addChild()将图像添加到容器中,因此显然这对于​​空数组不起作用。为每个图像创建一个新的Sprite并首先将其添加到数组中:

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"}));
}