Search code examples
javascriptarraysactionscript-3loaderloaderinfo

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


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);   

My onComplete event handler shows this:

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

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]);

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

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

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]);
}

Solution

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

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