如何解决在ActionScript 3闭合问题(AS3) [英] How to fix closure problem in ActionScript 3 (AS3)

查看:142
本文介绍了如何解决在ActionScript 3闭合问题(AS3)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在code下面我试图加载某些图像,并把他们在舞台上,只要他们得到单独加载。但由于只显示最后一个图像被窃听。我怀疑这是一个封闭的问题。我该如何解决这个问题?是不是封闭的AS3的行为等同于Java脚本?

  VAR图像列表:阵列=新的Array();
imageList.push({SRC:image1.jpg'});
imageList.push({SRC:image2.jpg'});
VAR imagePanel:影片剪辑=新的MovieClip();
this.addChildAt(imagePanel,0);

为(在图像列表变种I){
    VAR为imageData =图像列表[I]
    imageData.loader =新的Loader();

    imageData.loader.contentLoaderInfo.addEventListener(
        引发Event.COMPLETE,
        功能() {
            imagePanel.addChild(imageData.loader.content为位图);
            追踪(已完成:'+ imageData.src);
        });

    跟踪(开始:+ imageData.src);
    imageData.loader.load(新的URLRequest(imageData.src));
}
 

解决方案
  

是不是倒闭的AS3的行为等同于Java脚本?

是的,JavaScript的不完全一样的东西。一样的Python。和其他人。

尽管定义里面的'为''VAR为imageData',for循环不引入这些语言的一个新的范围;事实上变量为imageData被绑定在包含范围内(外功能,或在这种情况下,它似乎是全球范围内)。您可以通过查看为imageData后循环已经执行完毕,并找到其图像列表的最后一个元素验证这一点。

所以只有一个为imageData变量,而不是一个用于循环的每次迭代。完成后触发,进入关闭和读取任何价值为imageData拥有的现在的,不是当时的函数定义(*)。通常,for循环将已经由点COMPLETE火灾完成,为imageData将持有的最后一个元素的最后一次迭代。

(* - 。存在早期绑定语言说的将会的计算变量的值,你定义一个封闭但ActionScript是不是其中之一了吧)

可能的解决方案往往使用外部功能引入新的范围涉及。例如:

 函数makeCallback(为imageData){返回函数(){
    imagePanel.addChild(imageData.loader.content为位图);
    追踪(已完成:'+ imageData.src);
}}
...
imageData.loader.contentLoaderInfo.addEventListener(引发Event.COMPLETE,makeCallback(为imageData));
 

您/可/把这个内联,但双重嵌套函数()开始变得难以阅读。

也可以使用才达到这个通用的部分功能应用功能见Function.bind()。它很可能是未来的JavaScript /动作版本的部分,并且可以通过成型于其间添加到语言

In the code below I'm trying to load some images and put them in the stage as soon as they get individually loaded. But it is bugged since only the last image is displayed. I suspect it's a closure problem. How can I fix it? Isn't the behaviour of closures in AS3 the same as in Java Script ?

var imageList:Array = new Array();
imageList.push({'src':'image1.jpg'});
imageList.push({'src':'image2.jpg'});
var imagePanel:MovieClip = new MovieClip();
this.addChildAt(imagePanel, 0);

for (var i in imageList) {
    var imageData = imageList[i];
    imageData.loader = new Loader();

    imageData.loader.contentLoaderInfo.addEventListener(
        Event.COMPLETE, 
        function() {
            imagePanel.addChild(imageData.loader.content as Bitmap);
            trace('Completed: ' + imageData.src);             
        });

    trace('Starting: ' + imageData.src);
    imageData.loader.load(new URLRequest(imageData.src));   
}

解决方案

Isn't the behaviour of closures in AS3 the same as in Java Script ?

Yes, JavaScript does exactly the same thing. As does Python. And others.

Although you define 'var imageData' inside the 'for', for loops do not introduce a new scope in these languages; in fact the variable imageData is bound in the containing scope (the outer function, or in this case it appears to be global scope). You can verify this by looking at imageData after the loop has completed executing, and finding the last element of imageList in it.

So there is only one imageData variable, not one for each iteration of the loop. When COMPLETE fires, it enters the closure and reads whatever value imageData has now, not at the time the function was defined(*). Typically the for-loop will have finished by the point COMPLETE fires and imageData will be holding that last element from the final iteration.

(* - there exist 'early-binding' languages that will evaluate the variable's value at the point you define a closure. But ActionScript is not one of them.)

Possible solutions tend to involve using an outer function to introduce a new scope. For example:

function makeCallback(imageData) { return function() {
    imagePanel.addChild(imageData.loader.content as Bitmap);
    trace('Completed: ' + imageData.src);                                                                                                     
} }
...
imageData.loader.contentLoaderInfo.addEventListener(Event.COMPLETE, makeCallback(imageData));

You /can/ put this inline, but the doubly-nested function() starts to get harder to read.

See also Function.bind() for a general-purpose partial function application feature you could use to achive this. It's likely to be part of future JavaScript/ActionScript versions, and can be added to the language through prototyping in the meantime.

这篇关于如何解决在ActionScript 3闭合问题(AS3)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆