AS3 flash事件跳跃令我困惑

时间:2011-08-10 03:31:07

标签: flash actionscript-3 flash-cs5

我正在AS3中编写一个类,它会在舞台上显示一些缩略图。

我正在使用一堆事件来上传图像并将它们制作成位图并最终将它们放在舞台上,但它变得非常非常混乱。

首先,我让用户选择几张图片。说5张图片。然后我加载它们,就像这样:

//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执行此操作:

//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
}

最后,这个:

//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是一个全局变量。

所以这是最初的for-loop将进入的最深刻的步骤。如果我在循环完成后跟踪bmpArray的长度,它将始终显示为0.实际上,如果我这样做:

//Wait for bmpArray to be filled.

var arrayIsFull:Boolean = false;

while(!arrayIsFull) {
    trace(bmpArray.length);
    trace(numberFiles);
    if(bmpArray.length == numberFiles) {
        arrayIsFull = true;
    }
}

然后bmpArray永远保持在0。 Flash停止调试,因为在运行相同脚本15秒后,它会自动停止。

这里发生了什么?为什么bmpArray保持空白?

我在推动后立即追踪了阵列的长度。我已经看到它到达代码的那一部分并增加到实际数量。那么为什么循环后它会变空?

编辑:有问题的2个数组已在类级别声明。不在函数内部。范围不应该是问题:

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

2 个答案:

答案 0 :(得分:0)

您无法访问已在函数内声明的变量(来自函数外部)。请查看variable scope

答案 1 :(得分:0)

可能是,你在发送异步任务后忙着等待吗?你知道flash是单线程的。也许繁忙的等待阻止了事件的处理?

为什么不在处理程序方法中执行跟踪?

相关问题