2012-06-13 67 views
0

我有圖片,我需要循環扔得到URL並通過此功能的LoadImage加載它們,功能工作正常的陣列和加載圖像把我的問題與MovieClip 當我將圖像添加到它的圖像get添加到循環中的最後一個Movieclip?傳遞變量引用到功能

var relatedJSON = JSON.decode(e.target.data); 
    var relatedStories = relatedJSON.stories; 
    if(relatedStories.length > 0){ 
     for(var j:Number=0;j<relatedStories.length;j++){ 
      if(relatedStories[j].yahki_thumb){ 
       var one = j+1; 
       var block = related_stories.getChildByName('related_stories_block'+one); 
       // load the image 
       loadImage({ 
        path:relatedStories[j].yahki_thumb, 
        success:function(e:Event,my_loader:Loader) 
        { 
         my_loader.width = this[block].width; 
         my_loader.height = this[block].height; 
         this[block].img.addChild(my_loader); 
        }, 
        error:function(e:Event) 
        { 
         // error 
         echo('error to load an image'); 
        } 
       }); 
      } 
     } 
     // replay button event 
     //replay 
    }else{ 
     // error 
     echo('no related stories'); 
    } 

回答

1

循環中試試這個:

var that:* = this; 
    (function(block:*):void{ 
     loadImage({ 
       path:relatedStories[j].yahki_thumb, 
       success:function(e:Event,my_loader:Loader) 
       { 
        my_loader.width = that[block].width; 
        my_loader.height = that[block].height; 
        that[block].img.addChild(my_loader); 
       }, 
       error:function(e:Event) 
       { 
        // error 
        echo('error to load an image'); 
       } 
      }); 
    })(block); 

由於加載asyncronous,當你成功的函數被調用,block數值已經related_stories.getChildByName('related_stories_block'+relatedStories.length)價值。這是原因,元素被添加到最後MovieClip