最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin4浏览0评论

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

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]);
}
Share Improve this question edited Jan 14, 2017 at 7:45 user4535610 asked Sep 16, 2016 at 13:00 ChowzenChowzen 3213 silver badges13 bronze badges 3
  • Have you considered creating custom class based on URLLoader and store the information there? – Petr Hrehorovsky Commented Sep 16, 2016 at 13:04
  • @PetrHrehorovsky Never made a custom class, wouldn't know where to even start on that... Are you saying that I can't get the "i" number from loaderInfo? – Chowzen Commented Sep 16, 2016 at 13:10
  • 1 The source of your problem is distributing connected objects across several arrays. – null Commented Sep 16, 2016 at 20:22
Add a ment  | 

2 Answers 2

Reset to default 8

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

Since posting this question, I've been utilizing the following class. It takes in an integer (number pictures to load) and gives public access to an array of Sprites in the Array "ShapeArr."

Each sprite's name property is derived from its URL name. (name:"pic1" from loaded url "assets/pic1.png")

I was having trouble with the whole concept/implementation of inline functions, and have been using this approach instead.

package {
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.display.Loader;
import flash.URLRequest;
import flash.display.Sprite;
import flash.events.Event;

public class MultipleImageLoader extends Sprite {
    {private var pic:Array = [
        "assets/pic1.png",  "assets/pic2.png",  "assets/pic3.png",  "assets/pic4.png",
    ]}

    private var loader:Array = [];

    public var ShapeArr:Array = [];
    public var bitmapDataArr:Array = [];
    public var bmVisArr:Array = [];     
    private var shapeText:Array = [];       
    private var picArray:Array = [];

    private var count:int = 0;
    private var loaderCounter:int = 0;
    private var numPicsToLoad:int;
    private var a:String;
    public var loaded:Boolean = false;

    public function MultipleImageLoader(numPics:int):void {
        numPicsToLoad = numPics;
        loaded = false;

        init();
    }

    private function init(e:Event = null):void {
        if (hasEventListener(Event.ADDED_TO_STAGE)) {
            removeEventListener(Event.ADDED_TO_STAGE, init);
        }
        picArray = new Array;
        for (var i:int = 0; i < numPicsToLoad; i++) {
            picArray.push(i);
        }
        initiateLoaders();
    }

    private function initiateLoaders():void{
        loader[loaderCounter] = new Loader;
        loader[loaderCounter].contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete, false, 0, true);

        a = pic[picArray[loaderCounter]];
        //trace("shapecolor load:", a);
        shapeText[loaderCounter] = (a.substr(16, a.length - 20));
        loader[loaderCounter].load(new URLRequest(a ) );
    }

    private function onComplete(e:Event):void {
        //trace("sssssssssssssssssssssssssshapecolor");
        bitmapDataArr[loaderCounter] = e.target.content.bitmapData;
        bmVisArr[loaderCounter] = new Bitmap(bitmapDataArr[loaderCounter]);
        bmVisArr[loaderCounter].scaleX = .1;
        bmVisArr[loaderCounter].scaleY = .1;
        bmVisArr[loaderCounter].x =-bmVisArr[loaderCounter].width / 2;
        bmVisArr[loaderCounter].y =-bmVisArr[loaderCounter].height / 2;

        ShapeArr[loaderCounter] = new Sprite();
        ShapeArr[loaderCounter].name = a.substr(7,4);

        trace("Name",loaderCounter,ShapeArr[loaderCounter].name );

        ShapeArr[loaderCounter].addChild(bmVisArr[loaderCounter]);
        loader[loaderCounter].contentLoaderInfo.removeEventListener(Event.COMPLETE, onComplete);
        if (loaderCounter <numPicsToLoad-1) {
            loaderCounter += 1;
            initiateLoaders();
        }
        //trace("gonna count",count);
        counting();
        count += 1;
    }

    private function counting():void {
        trace("tile count", count,numPicsToLoad);
        if (count < numPicsToLoad-1) {
            return;
        }
        else{
            removeEventListener(Event.ENTER_FRAME, counting);
            loaded = true;
            count = 0;
            trace("All Images LOADED");
        }
    }   
}//end  Class
}//end Package
发布评论

评论列表(0)

  1. 暂无评论