来自图像加载器内创建的位图的null值

时间:2018-05-18 17:02:46

标签: actionscript-3

我有一个从库中获取图像的功能,并在加载后将它们呈现在舞台上。

function addStuff(){
    this["otherstuff" + index] = new Textfield;
    addChild(this["otherstuff" + index]);

    var IMAGE_URL:String = arraywithstuff[index][2];
    var ldr:Loader = new Loader();
    ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, ldr_complete);
    ldr.load(new URLRequest(IMAGE_URL));

    function ldr_complete(evt:Event):void {
        var bmp:Bitmap = ldr.content as Bitmap;
        this["photo" + index] = new Bitmap(bmp.bitmapData);
        addchild(this["photo" + index]);

然后我稍后尝试删除另一个功能中的照片

function removeStuff(){
    for (var i = 0; i<maxindex; i++){
        removeChild(this["otherstuff" + i]);
        removeChild(this["photo" + i]);
    }

这适用于我在加载器之外创建的东西,比如文本框,但图像不会消失。我收到此错误:Parameter child must be non-null.

1 个答案:

答案 0 :(得分:2)

// Create the array.
var aList:Array = new Array;

function addStuff()
{
    // ...
    // Your code.
    var aLoader:Loader = new Loader;
    aLoader:Loader.contentLoaderInfo.addEventListener(Event.INIT, onBitmap);
    aLoader:Loader.load(new URLRequest(IMAGE_URL));
    // Your code.
    // ...
}

// Don't declare methods inside other methods.
function onBitmap(e:Event):void
{
    var anInfo:ContentLoaderInfo = e.target;
    var aRaster:Bitmap = anInfo.content as Bitmap;

    // Unsubscribe the listener.
    anInfo.removeEventListener(Event.INIT, onBitmap);

    // Store the reference.
    aList.push(aRaster);
    addChild(aRaster);
}

function deleteStuff():void
{
    while (aList.length)
    {
        var aRaster:Bitmap = aList.pop();

        removeChild(aRaster);

        // Release the loaded data.
        aRaster.bitmapData.dispose();
    }
}