当我尝试在 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"))
}
现在不同的位图和加载器具有不同的名称,并在同一个方法调用中调用。事件处理程序现在是内联的。
我无法弄清楚错误在哪里。