2013-02-08 59 views
1

我已經創建了一個容器(或影片剪輯),將按順序添加照片並顯示在舞臺上。但是,由於文件大小不同,它會首先添加最小的文件大小照片。我怎麼能解決這個問題。請參閱下面照片加載順序亂了as3

// image0.jpg -> 3k 
// image1.jpg -> 2k 
// image2.jpg -> 1k 
// image3.jpg -> 2k 
// image5.jpg -> 1k 

var photoPath:Array = new Array(); 
var photoContainer:MovieClip = new MovieClip(); 

photoPath.push('image0.jpg'); 
photoPath.push('image1.jpg'); 
photoPath.push('image2.jpg'); 
photoPath.push('image3.jpg'); 
photoPath.push('image4.jpg'); 

for(var i = 0; i < photoPath.length; i++) 
{ 
    var loader:Loader = new Loader(); 
    loader.load(URLRequest(photoPath[i])); 
    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, photoLoaded); 
} 

function photoLoaded(e:Event):void 
{ 
    photoContainer.addChild(e.target.content); 
} 

//output will looks like (image2,image5,image1,image3,image0) instead of 0,1,2,3,4,5 

回答

1

示例代碼中有相當多的方式來處理這個問題,可能是你改造現有的代碼最簡單的方法是,因爲它被實例化的裝載機添加到顯示列表。這將確保它們以正確的順序放入photoContainer中。如果你絕對需要的位圖本身是在顯示列表,而不是裝載機本身,你可以在你的photoLoaded方法解決這個問題:

import flash.display.Loader; 

var photoPath:Array = new Array(); 
var photoContainer:MovieClip = new MovieClip(); 

photoPath.push('image0.jpg'); 
photoPath.push('image1.jpg'); 
photoPath.push('image2.jpg'); 
photoPath.push('image3.jpg'); 
photoPath.push('image4.jpg'); 

for(var i = 0; i < photoPath.length; i++) 
{ 
    var loader:Loader = new Loader(); 
    photoContainer.addChild(loader); //add the loader to photoContainer 
    loader.load(new URLRequest(photoPath[i])); 
    //you only actually have to listen for complete below if you want to replace the loader with its contents 
    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, photoLoaded); 
} 

function photoLoaded(e:Event):void 
{ 
    var loader:Loader = e.target.loader; 
    var index:int = photoContainer.getChildIndex(loader); 
    // this will add the contents of the loader in the same order the loader was originally added 
    photoContainer.addChildAt(loader.content, index); 
    photoContainer.removeChild(loader); 
} 
0

簡單的負載,並增加了照片的容器在給定的順序陣列:

var photoPath:Array = new Array(); 
var photoContainer:MovieClip = new MovieClip(); 
var i:int = 0; 

photoPath.push('image0.jpg'); 
photoPath.push('image1.jpg'); 
photoPath.push('image2.jpg'); 
photoPath.push('image3.jpg'); 
photoPath.push('image4.jpg'); 

loadPhoto(i); 

function loadPhoto(i:int):void 
{ 
    var loader:Loader = new Loader(); 
    loader.load(new URLRequest(photoPath[i])); 
    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, photoLoaded); 
} 

function photoLoaded(e:Event):void 
{ 
    photoContainer.addChild(e.target.content); 
    trace(e.target.url); // Verify 
    if(i<photoPath.length-1) loadPhoto(++i); 
} 

輸出:

image0.jpg 
image1.jpg 
image2.jpg 
image3.jpg 
image4.jpg