gpt4 book ai didi

javascript - AS3 : How do I get dynamic loader URL from LoaderInfo in Event Listener Function?

转载 作者:可可西里 更新时间:2023-11-01 02:38:20 25 4
gpt4 key购买 nike

我正在加载很多图片,并且正在使用一个数组来执行此操作。

loader[i].load(new URLRequest(picture[i]));

我的事件监听器功能是这样启用的:

loader[i].contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);   

我的 onComplete 事件处理程序显示了这一点:

trace(e.target); //OUTPUT: [object LoaderInfo]

我在 LoaderInfo 中寻找了一些属性,这些属性可能会识别哪个加载器启动了监听器(“i”的值),这样我就可以具体地处理每个加载器,如下所示:

bitmapDataArr[i] = e.target.content.bitmapData;
bmVisArr[i] = new Bitmap(bitmapDataArr[i]);

但无法确定是哪个“i”发起了监听器的具体实例。

有什么想法吗?我尝试给 LoaderInfo 命名,但无济于事。我仍然无法提取讨厌的小识别码。

编辑 显示加载器循环和 onComplete 函数:

for (i = 0; i < 10; i++) {
loader[i] = new Loader();
loader[i].contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete, false, 0, true);
loader[i].load(new URLRequest(letter[i]));
}

private function onComplete(e:Event):void {
trace("e.target",e.target); //OUTPUT: e.target [object LoaderInfo]
var LI:LoaderInfo = e.target as LoaderInfo;
var eNum:int = (????);
bitmapDataArr[eNum] = e.target.content.bitmapData;
bmVisArr[eNum] = new Bitmap(bitmapDataArr[eNum]);
}

最佳答案

您需要以某种方式将 i 值传递给 onComplete 函数。例如,在 this 上下文中或通过参数。

P.S.:使用弱引用更容易。字典而不是删除属性,虽然我对 AS3 了解不多。

下面的示例还展示了如何删除事件监听器(包括它们的回调函数):

/* An object containing callback
* functions used along with event listeners.
*/
const callbacks: Object = {};


/* This function will re-declare and hoist i
* in itself. */
private function loop(i: uint): void {
loader[i] = new Loader;

const wrapped =
callbacks[i] = function wrapper(...args) {
// Pass all arguments (respectively event and i)
onComplete.apply(null, args);

// Function#apply(thisContext, arguments)
// Rest exp. isn't implemented yet, else we could just do:
// onComplete(...args);
};

loader[i].contentLoaderInfo
.addEventListener(Event.COMPLETE, wrapped, false,
0, true);

loader[i].load(new URLRequest(letter[i]));
};

for (var i: uint = 0; i < 10; ++i) loop(i);

private function onComplete(e: Event, i: uint): void {
const loaderInfo: LoaderInfo = e.target as LoaderInfo;

bitmapDataArr[i] = e.target
.content.bitmapData;

bmVisArr[i] = new Bitmap(bitmapDataArr[i]);

loader[i].contentLoaderInfo
.removeEventListener(
Event.COMPLETE, callbacks[i]
);

// Deletes the property that stores
// the function inside callbacks
delete callbacks[i];
}

关于javascript - AS3 : How do I get dynamic loader URL from LoaderInfo in Event Listener Function?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39532177/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com