2010-06-10 21 views
0

我試圖動態堆疊正在通過xml文件拉入圖像。以下是我正在做的事情,它幾乎可行。問題在於,它似乎只是在最後一個事件中觸發事件完成功能,而不是全部實現它們。有沒有辦法讓它爲每個圖像運行even.complete功能?Flash AS3:根據圖像高度從循環位置加載圖像

function aboutfileLoaded(event:Event):void { 
    aboutXML = new XML(aboutTextLoader.data); 
for(var l:int = 0; l < aboutXML.aboutimages.image.length(); l++) 
    { 
      imageLoader = new Loader(); 
      imageSource = aboutXML.aboutimages.image[l]; 

      if (imageSource != "") { 
        this.imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, aboutimageLoaded); 
        this.imageLoader.load(new URLRequest(imageSource)); 

        //aboutBox.aboutContent.addChild(imageLoader); 
        //imageLoader.y = imageYpos; 
        //imageYpos = imageYpos + 50; 
      } 

    } 
} 

function aboutimageLoaded(event:Event):void { 
      aboutBox.aboutContent.addChild(imageLoader); 
      this.imageLoader.y = imageYpos; 
      imageYpos = imageYpos + this.imageLoader.height; 
} 

回答

0

我使用了一個簡單的Image類,用於加載所有圖像。

public function loadImages(xml:XML):void { 
    for each(var img:XML in xml.images) { 
     var i:Image = new Image(img.src, img.width, img.height); 

     i.loader.contentLoaderInfo.addEventListener(Event.COMPLETE, positionImage); 
    } 
} 

/**/ 

private function positionImage(e:Event):void { 
    var i:Image = e.target; 
    gallery.addChild(i); 
    // Do the positioning. 
} 

在上面的代碼中,你總是可以只改變你的aboutimageLoaded功能:

// In aboutfileLoaded() change all "this.imageLoader" to just "imageLoader" 

function aboutimageLoader(event:Event):void { 
    aboutBox.aboutContent.addChild(event.target); 
    event.target.y = imageYpos; 
} 
+0

改變事件event.target給我的錯誤「隱式強制的靜態類型對象的值到一個可能不相關的類型Flash:DisplayObject」源「aboutBox.aboutContent.addChild(event.target);」 – HeroicNate 2010-06-10 07:30:11

+0

糟糕,只是爲了這裏的演示目的。我通常只是在所有「gallery.addChild(e.target)」 – 2010-06-10 07:38:48

+0

中使用e.target,就像我上面所說的那樣,使用event.target會給我那個錯誤,如果我註釋掉addChild這行,我會爲.y和位置。 – HeroicNate 2010-06-10 16:03:59

0

每個Loader可以只處理一次一個工作,所以無論你想堆作業加載一個接一個:

//Global vars 
loadIndex:int = 0; 
imageLoader = new Loader(); 
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, aboutimageLoaded); 
loadImage(); 

private function loadimage(){ 
    if(aboutXML.aboutimages.image[loadIndex] != null){ 
     imageSource = aboutXML.aboutimages.image[loadIndex]; 
     if (imageSource != "") { 
      imageLoader.load(new URLRequest(imageSource)); 
     } 
    } 
} 

function aboutimageLoaded(event:Event):void { 
    var holder = (ev.content as Bitmap).clone(); 
    aboutBox.aboutContent.addChild(holder); 
    holder .y = imageYpos; 
    imageYpos = imageYpos + holder.height; 
    loadIndex ++; 
    loadimage(); 
} 

或使裝載機的多個實例,每一個:

for(var l:int = 0; l < aboutXML.aboutimages.image.length(); l++) 
    { 
      var imageLoaderTemp = new Loader(); 
      imageSource = aboutXML.aboutimages.image[l]; 

      if (imageSource != "") { 
        imageLoaderTemp.contentLoaderInfo.addEventListener(Event.COMPLETE, aboutimageLoaded); 
        imageLoaderTemp.load(new URLRequest(imageSource)); 
      } 
    } 
} 

function aboutimageLoaded(event:Event):void { 
      aboutBox.aboutContent.addChild(event.target); 
      event.target.y = imageYpos; 
      imageYpos = imageYpos + event.target.height; 
} 
+0

你的第二個選擇對我來說似乎很理想,但它不起作用。我得到了「未定義財產的存取權」。或者,如果我將其更改爲事件,則會爲addChild獲得「隱式強制使用靜態類型Object的值」。 – HeroicNate 2010-06-10 07:18:54

+0

啊,我的道歉,把「ev」替換成「event」,編輯爲 – longstaff 2010-06-10 08:11:58

+0

,使用event.target給我錯誤。 – HeroicNate 2010-06-10 16:03:10