2013-04-09 60 views
0

因此,我有一個LoaderMax實例從各種URL加載圖像。我想將所有加載的圖像添加到數組中。 這裏是我的代碼:LoaderMax:將數組設置爲容器(ImageLoader)

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,它有點作用(但這不是理想的)。問題是,通過這樣做,圖像在加載時出現在舞臺上,我不希望它們這樣做。

任何幫助將不勝感激。 謝謝!

回答

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

不錯......我會試着去看看它是怎麼回事。謝謝,大衛! – yvesbastos 2013-04-10 17:44:47

2

大衛是正確的,但我也想提一提的是,LoaderMax的「內容」,實際上是所有兒童的內容的數組,所以你可以簡單地使用它。請記住,ImageLoaders會自動創建一個Sprite(技術上稱爲「ContentDisplay」)來放置圖像,因此您可能不需要創建另一個Sprite(容器的容器)。

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

另一個好處是,它會立刻創建ContentDisplay精靈,甚至在任何內容加載到他們,所以你可以把它們和大小它們不過你想要的,同時(或之前或之後)裝載發生。

+0

我會嘗試進行這些更改,看看它是如何發生的。謝啦! – yvesbastos 2013-04-10 17:44:10

+0

真棒,我應該知道那裏已經有一個優雅的解決方案! – 2013-04-10 18:41:28