Flash: Loading multiple images and the expectation of their full load
I need to load multiple images from ZIP archive and do some actions after all files from ZIP archive have been loaded. I use FZip library to manipulate with ZIP files. My problem is that sometimes the loading of all files are not going.
In this code I starting loading process:
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开发者_如何学JAVA);
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();
}
}
Sometimes not all images downloaded and I have this trace output:
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
It is seen that all Loader starts, but not all finish. No exceptions happens and it's happens sometimes. I don't understand why.
Try queuing up your loads and executing them one at a time (i.e. don't start loading image_1 until image_0 is finished). Something like,
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();
}
I've seen similar behavior myself and queuing the loads solved the issue. I think what might be going on is that Flash uses only a single LoaderInfo object across all Loader objects (don't quote me on this) and when you attempt to load a series of files like that it can behave a bit erratically. Good luck!
精彩评论