AS3:如何从事件侦听器功能中的LoaderInfo获取动态加载程序URL? [英] AS3: How do I get dynamic loader URL from LoaderInfo in Event Listener Function?

查看:77
本文介绍了AS3:如何从事件侦听器功能中的LoaderInfo获取动态加载程序URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在加载许多图片,并且正在使用数组来这样做.

I'm loading many pictures, and am using an array to do so.

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

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

My Event Listener function is enabled like this:

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

我的onComplete事件处理程序显示如下:

My onComplete event handler shows this:

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

我已经在LoaderInfo中寻找了一些属性,这些属性可以标识哪个加载器启动了侦听器("i"的值),以便我可以对每个加载器进行专门的设置,如下所示:

I've looked for properties in LoaderInfo that might identify which loader initiated the listener (the value of "i") so that I can putz around with each one specifically, like this:

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

但是无法确定哪个"i"启动了侦听器的特定实例.

But cannot determine which "i" initiated the specific instance of the listener.

有什么想法吗?我尝试给LoaderInfo命名没有用.我仍然无法提取讨厌的小识别号.

Any ideas? I tried giving a name to LoaderInfo to no avail. I still can't extract the pesky little identifying number.

EDIT 显示加载程序和onComplete函数的循环:

EDIT showing loop for loaders and onComplete function:

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 上下文中或通过一个参数.

You'll somehow need to bring i value to onComplete function. For example, in the this context or thru an argument.

P.S .:使用弱引用会更容易.用字典代替删除属性,尽管我对AS3不太了解.

P.S.: It's easier to use weak ref. Dictionaries instead of deleting properties, though I don't know much about AS3.

下面是一个示例,还显示了如何删除事件侦听器(包括其回调函数):

Here's an example that also shows how to remove the event listeners (including their callback functions):

/* 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];
}

这篇关于AS3:如何从事件侦听器功能中的LoaderInfo获取动态加载程序URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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