10

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

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

2 回答 2

8

您将需要以某种方式i为功能带来价值onComplete。例如,在this上下文中或通过参数。

PS:使用弱参考更容易。字典而不是删除属性,虽然我对 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];
}
于 2016-09-16T15:14:22.277 回答
4

自从发布这个问题以来,我一直在使用以下课程。它接受一个整数(要加载的图片数量)并提供对数组“ShapeArr”中的 Sprite 数组的公共访问权限。

每个精灵的名称属性都源自其 URL 名称。(名称:“pic1”来自加载的 url “assets/pic1.png”)

我在内联函数的整个概念/实现方面遇到了麻烦,并且一直在使用这种方法。

package {
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.display.Loader;
import flash.net.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
于 2017-01-16T14:55:19.350 回答