I'm writing a class in AS3 which will display some thumbnails on the stage.

I'm using a bunch of events to upload the images and make them into bitmaps and finally place them on stage, but it's becoming very, very confusing.

First, I have the user select a couple of images. Say 5 images. I then load them, like this:

//Files in an array to use in loop var files:Array = imgList.fileList; numberFiles = imgList.fileList.length; //Have all files be locally encoded //for-in loop gives too much headaches for(var i:Number = 0; i<numberFiles; i++){ var fileRef:FileReference = files[i]; fileRef.addEventListener(Event.COMPLETE, imageLoadHandler); fileRef.load(); }

imageLoadHandler does this:

//Turn the image in to a bitmap. private function imageLoadHandler(evt:Event):void { var loader:Loader = new Loader(); loader.contentLoaderInfo.addEventListener(Event.COMPLETE, bitmapLoadHandler); loader.loadBytes(evt.target.data);//security issue }

and finally, this:

//have the bitmap configured with width and height and scaling. private function bitmapLoadHandler(evt:Event):void { var bmp:Bitmap = Bitmap(evt.target.content); //width, height and scaling bmp.width = THUMBWIDTH; bmp.height = THUMBHEIGHT; bmp.scaleX < bmp.scaleY ? bmp.scaleY = bmp.scaleX : bmp.scaleX = bmp.scaleY; //push the image on to an array bmpArray.push(bmp); }

bmpArray is a global variable.

So this is the deepest step that the original for-loop will get in to. If I then do a trace of the length of the bmpArray after the loop is done, it will always show 0. In fact, if i do this:

//Wait for bmpArray to be filled. var arrayIsFull:Boolean = false; while(!arrayIsFull) { trace(bmpArray.length); trace(numberFiles); if(bmpArray.length == numberFiles) { arrayIsFull = true; } }

Then the bmpArray stays at 0 forever. Flash stops debugging because after 15 seconds of the same script running it stops automatically.

What is going on here? Why is bmpArray staying empty?

I've put up a trace for the length of the array right after the push. I've seen that it reaches that part of the code and increases to the actual amount. So why is it empty after the loop?

EDIT: the 2 arrays in question have been been declared at class-level. Not inside the function. Scope should not be the issue: