2010-02-26 65 views
1

我需要從ZIP壓縮文件中加載多個圖像,並在加載ZIP壓縮文件中的所有文件後執行一些操作。我使用FZip庫來處理ZIP文件。我的問題是,有時候所有文件的加載都不會執行。Flash:加載多張圖片及其滿載的期望

在這段代碼我開始加載過程:

loadedImages = zip.getFileCount(); 
trace("Starting... " + loadedImages); 
for (var i:uint = 0; i < loadedImages; i++) 
{ 
    var file:FZipFile = zip.getFileAt(i); 
    // NamedLoader - common flash.display.Loader who remembers the name of the file 
    var pictLdr:Loader = new NamedLoader(file.filename); 
    trace("start " + i); 
    pictLdr.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded); 
    pictLdr.loadBytes(file.content, new LoaderContext(false, ApplicationDomain.currentDomain)); 
} 

private function imageLoaded(e:Event):void 
{ 
    // do some actions, like getting file content and store it 
    trace("Loaded: " + loadedImages); 
    loadedImages--; 
    // if all images has been loaded, I show some scene 
    if (loadedImages == 0) 
    { 
     showScene(); 
    }  
} 

有時候,不是所有的圖片下載的,我有這樣的跟蹤輸出:

Starting... 36 
start 0 
start 1 
start 2 
start 3 
start 4 
start 5 
start 6 
start 7 
start 8 
start 9 
start 10 
start 11 
start 12 
start 13 
start 14 
start 15 
start 16 
start 17 
start 18 
start 19 
start 20 
start 21 
start 22 
start 23 
start 24 
start 25 
start 26 
start 27 
start 28 
start 29 
start 30 
start 31 
start 32 
start 33 
start 34 
start 35 
Loaded: 36 
Loaded: 35 
Loaded: 34 
Loaded: 33 
Loaded: 32 
Loaded: 31 
Loaded: 30 
Loaded: 29 
Loaded: 28 
Loaded: 27 
Loaded: 26 
Loaded: 25 
Loaded: 24 
Loaded: 23 
Loaded: 22 
Loaded: 21 
Loaded: 20 
Loaded: 19 
Loaded: 18 
Loaded: 17 
Loaded: 16 
Loaded: 15 
Loaded: 14 
Loaded: 13 

可以看出,所有的裝載機開始,但不是所有的光潔度。沒有例外發生,有時會發生。我不明白爲什麼。

+0

您是否嘗試過使用Fiddler或Firebug來查看發生故障的請求的情況? – 2010-02-26 05:00:43

+0

是的,我試過。如果我關閉瀏覽器緩存,那麼發送GET查詢,並且答案是OK(200)。但有時圖像不能完全加載。 – 2010-02-26 06:50:45

回答

3

嘗試排隊您的負載並逐個執行它們(即,在image_0完成之前不要開始加載image_1)。類似的,

function createQueue():void 
{ 
    // queue up your loads 
    for (var i:uint = 0; i < loadedImages; i++) 
    { 
     var file:FZipFile = zip.getFileAt(i); 
     // NamedLoader - common flash.display.Loader who remembers the name of the file 
     var pictLdr:Loader = new NamedLoader(file.filename); 
     trace("start " + i); 
     pictLdr.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded); 
     queue.push({loader: pictLdr, file: file}); 
    } 

    loadNext(); 
} 

function loadNext():void 
{ 
    if (queue.length) { 
     var obj:Object = queue.shift(); 
     var file:FZipFile = obj.file; 
     var pictLdr:NamedLoader = obj.loader; 
     pictLdr.loadBytes(file.content, new LoaderContext(false, ApplicationDomain.currentDomain)); 

    } 
} 

private function imageLoaded(e:Event):void 
{ 
    // do some actions, like getting file content and store it 
    trace("Loaded: " + loadedImages); 
    loadedImages--; 
    // if all images has been loaded, I show some scene else load next image 
    loadedImages ? loadNext() : showScene(); 
} 

我已經看到了類似的行爲,並排隊加載解決了問題。我認爲可能發生的情況是,Flash在所有Loader對象中只使用一個LoaderInfo對象(不要在此引用我),並且當您嘗試加載一系列文件時,它可能表現得有點不正常。祝你好運!

+0

它確實有效。謝謝! – 2010-02-27 05:24:03