Note that there are some explanatory texts on larger screens.

plurals
  1. POParallel image loading in a single class instance with AS3
    text
    copied!<p>My problem occurs when I try to load multiple images in one class in AS3.</p> <p>My code looks something like this:</p> <pre><code>// 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")) </code></pre> <p>My problem with this is, that every complete-method receives the same image. Sometimes they all get <code>img1.jpg</code>, sometimes they all get <code>img3.jpg</code>….</p> <p>I don't have any clue why this is happening.</p> <p>I'm thankful for any help you can give me.</p> <p>This is the exact code that I used:</p> <p>Class <code>CuboidBookView</code></p> <pre><code>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); } </code></pre> <p>These methods are called in another class:</p> <pre><code>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"); </code></pre> <p>Edit: I thought, this would help, but it had the same result:</p> <pre><code>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")) } </code></pre> <p>Now the different Bitmaps and Loaders have distinct names and are called within the same method call. The event handlers now are inline.</p> <p>I cannot figure out where the mistake is.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload