开发者

AS3 flash event hopping confusing me

开发者 https://www.devze.com 2023-03-27 02:45 出处:网络
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 be

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 par开发者_开发问答t 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:

public class Pictures extends Sprite {
...
private var numberFiles:Number;
private var bmpArray:Array = new Array();


you can't access a variable (from outside of the function) that has been declared inside of a function. Check with variable scope.


Could it be, you do a busy waiting after sending an async task? You know flash is single threaded. Maybe the busy waiting is preventing the events from being processed?

Why don't you do the traces in the handler methods?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号