R – ActionScript 3: ProgressEvent’s bytesLoaded property to Document Class

actionscriptapache-flexeventsflashprogress

in AS3, I have an external class ImageLoader, that loads an image upon request. In that class, I have an event handlers:

ImageLoader Class

public function loadImg(path:String):void
{
 ldr = new Loader();
 ldr.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, getProgress);
 var req:URLRequest = new URLRequest(path);
 ldr.load(req);
}

private function getProgress(e:Event):void
{
 dispatchEvent(new Event("PROGRESS_INFO"));
}

I am trying to send the download progress updates back to the main Document Class and display it on screen, so I am trying to dispatch the event "PROGRESS_INFO" and then get the information from the passed Event Object, like so:

Document Class

private function getProgressInfo(e:Event):void
{
 trace(e.target.bytesTotal);
}

This however, is proving futile… any ideas on how I can get the progress info out of the IMageLoader class?

note: I know I can add bytesLoaded & bytesTotal to a public variable, but then I won't get the benefit of seeing the bytesLoaded property update in the ProgressEvent class. Any ideas?

Best Solution

Make sure your ImageLoader class extends the EventDispatcher class. Also, instead of creating a new event, you should re-dispatch the ProgressEvent.

private function getProgress(e:ProgressEvent):void
{
 dispatchEvent(e);
}

This should work in the document class:

myImageLoader = new ImageLoader();
myImageLoader.addEventListener(ProgressEvent.PROGRESS, getProgressInfo);

private function getProgressInfo(e:ProgressEvent):void
{
 trace(e);
}
Related Question