0

当我尝试在 AS3 的一个类中加载多个图像时,会出现我的问题。

我的代码看起来像这样:

// Load image 1
var ldr1:Loader = new Loader();
ldr1.contentLoaderInfo.addEventListener(Event.COMPLETE, complete1);
ldr1.load(new URLRequest("img1.jpg"));

// Load image 2
var ldr2:Loader = new Loader();
ldr2.contentLoaderInfo.addEventListener(Event.COMPLETE, complete2);
ldr2.load(new URLRequest("img2.jpg"));

// Load image 3
var ldr3:Loader = new Loader();
ldr3.contentLoaderInfo.addEventListener(Event.COMPLETE, complete3);
ldr3.load(new URLRequest("img3.jpg"))

我的问题是,每个完整方法都会收到相同的图像。有时他们都得到img1.jpg,有时他们都得到img3.jpg......

我不知道为什么会这样。

我很感谢你能给我的任何帮助。

这是我使用的确切代码:

班级CuboidBookView

public function addBackMaterial(url:String):void {
    var loader:Loader = new Loader();
    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onBMComplete);
    loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, function (e:IOErrorEvent):void {});

    loader.load(new URLRequest(url));
}
private function onBMComplete(e:Event):void {
    var bmp:Bitmap = Bitmap(LoaderInfo(e.target).content);
    backMaterial = new BitmapMaterial(bmp.bitmapData);
    hidden = false;
}

public function addFrontCoverMaterial(url:String):void {
    var loader:Loader = new Loader();
    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onFCMComplete);
    loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, function (e:IOErrorEvent):void {});

    loader.load(new URLRequest(url));
}
private function onFCMComplete(e:Event):void {
    var bmp:Bitmap = Bitmap(LoaderInfo(e.target).content);
    frontCoverMaterial = new BitmapMaterial(bmp.bitmapData);
}

public function addRearCoverMaterial(url:String):void {
    var loader:Loader = new Loader();
    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onRCMComplete);
    loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, function (e:IOErrorEvent):void {});

    loader.load(new URLRequest(url));
}
private function onRCMComplete(e:Event):void {
    var bmp:Bitmap = Bitmap(LoaderInfo(e.target).content);
    rearCoverMaterial = new BitmapMaterial(bmp.bitmapData);
}

这些方法在另一个类中调用:

CuboidBookView(book.view).addBackMaterial("../res/books/" + b.file + "_back.jpg");
CuboidBookView(book.view).addFrontCoverMaterial("../res/books/" + b.file + "_front.jpg");
CuboidBookView(book.view).addRearCoverMaterial("../res/books/" + b.file + "_rear.jpg");

编辑:我想,这会有所帮助,但结果相同:

public function addMaterials(name:String):void {
    // Load background image
    var ldr1:Loader = new Loader();
    ldr1.contentLoaderInfo.addEventListener(Event.COMPLETE, function (e:Event):void {               
        var bm:Bitmap = e.currentTarget.content as Bitmap;
        backMaterial = new BitmapMaterial(bm.bitmapData);
        hidden = false;
    });
    ldr1.load(new URLRequest("../res/books/" + name + "_back.jpg"));

    // Load front cover
    var ldr2:Loader = new Loader();
    ldr2.contentLoaderInfo.addEventListener(Event.COMPLETE, function (e:Event):void {
        var fcm:Bitmap = e.currentTarget.content as Bitmap;
        frontCoverMaterial = new BitmapMaterial(fcm.bitmapData);
    });
    ldr2.load(new URLRequest("../res/books/" + name + "_front.jpg"));

    // Load rear cover
    var ldr3:Loader = new Loader();
    ldr3.contentLoaderInfo.addEventListener(Event.COMPLETE, function (e:Event):void {
        var rcm:Bitmap = e.currentTarget.content as Bitmap;
        rearCoverMaterial = new BitmapMaterial(rcm.bitmapData);
    });
    ldr3.load(new URLRequest("../res/books/" + name + "_rear.jpg"))
}

现在不同的位图和加载器具有不同的名称,并在同一个方法调用中调用。事件处理程序现在是内联的。

我无法弄清楚错误在哪里。

4

2 回答 2

0

您的 complete1()、complete2() 和 complete3() 方法是什么样的?

我要做的是通过以下方式从加载器中保存位图。

private function complete1(e:Event):void
{
    var image:Bitmap = e.currentTarget.content as Bitmap;
}

然后使用这个位图来渲染你的图像。

显然为每个位图使用不同的变量。我通常有一个 BitmapData 字典,一旦加载就会保存。

如果您发布的代码正是您在程序中所拥有的,那么这应该可以正常工作。

于 2011-06-13T13:10:15.430 回答
0

There really was a problem with the implementation of the Cube in Away3d. Some callbacks were not save.

I found a solution in reimplementing the whole class.

于 2011-08-25T11:00:37.580 回答