views:

646

answers:

2

I am using this class to load multiple images synchronously. Somehow the loader doesn't trigger any event (Event.COMPLETE, ProgressEvent.PROGRESS), strangely I also don't get any errors (using FlashDevelop and Flex3 SDK).

package  
{
    import flash.display.Loader;
    import flash.display.Sprite;
    import flash.events.*;
    import flash.net.URLRequest;

    public class MultiImgLoader extends EventDispatcher
    {
     private var img_array:Array;
     public var images:Array;
     private var loader:Loader = new Loader();

     public function MultiImgLoader(img_array:Array) 
     {
      this.img_array = img_array;
      trace("[MultiImgLoader] about to load " + img_array.length);
      if (img_array.length > 0)
      {
       load(img_array[0]);
      }
     }

     private function load(img:String):void
     {
      trace("[MultiImgLoader] load " + img);
      loader.addEventListener(ProgressEvent.PROGRESS, progress);
      loader.addEventListener(Event.COMPLETE, this.ready);
      var req:URLRequest = new URLRequest(img);
      loader.load(req);
     }

     public function ready(ev:Event):void
     {
      var key:String = ev.target.contentLoaderInfo.url;
      trace("[MultiImgLoader] ready " + key);
      images.push( { key : ev.target } );
      if (img_array.length > images.length)
      {
       for (var i:int = 0; i < img_array.length; i++ )
       {
        if (img_array[i] == key)
        {
         load(img_array[i+1]);
        }
       }
      }
     }

     public function progress(ev:ProgressEvent):void
     {
      trace(ev.bytesLoaded);
     }

    }

}
+2  A: 

Ok, got it. This:

loader.addEventListener(ProgressEvent.PROGRESS, progress);
loader.addEventListener(Event.COMPLETE, this.ready);

should read this:

loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progress);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, ready);

I don't even know why the Loader-Class has the addEvenListener method - redundancy anyone?

saibotd
Loader has addEventListener because it inherits from EventDispatcher. While the Loader class does not directly emit events about the status of the loading, it does handle all of the usual UI events for itself including mouse events, display events (ADDED_TO_STAGE, ENTER_FRAME, etc), and keyboard events. It's just that events having to do with the loading of the content are handled by it's built-in instance of LoaderInfo called contentLoaderInfo.
Branden Hall
This makes sense ... somehow, Thank you!
saibotd
+1 answer helped me 7 months later :)
Colin
A: 

I had something similar, but then I changed my loader from a new Loader() to a URLLoader() and it worked with loader.addEventListener.

This page helped me: http://livedocs.adobe.com/flex/3/html/help.html?content=17%5FNetworking%5Fand%5Fcommunications%5F3.html

Ivar