Part 2: Using LoaderInfo Events to create reusable ImageLoader Class

Yesterday I created a post called ActionScript 3.0 LoaderInfo Useful Events which outlined some useful events in ActionScript 3.0 that can be used to listen for when things are open, loading, and complete.

I used that knowledge to create a custom class which loads an image. I used events to listen for the opening of the the request, the download progress, and then the completion of the download. This is a basic class which can be applied to almost anything, although I kept it very basic for now. I plan to build upon this class over time until I have a reusable preloader class that I can apply to any project based off this code.

ImageLoader.as

Actionscript:
  1. package {
  2.     import flash.display.*;
  3.     import flash.text.*;
  4.     import flash.net.URLRequest;
  5.     import flash.events.*;
  6.    
  7.     public class LoaderExample extends Sprite {
  8.        
  9.         public var loaderStatus:TextField;
  10.        
  11.         public function LoaderExample() {
  12.             //Create the Loader and add it to the display list
  13.             var loader:Loader = new Loader();
  14.             addChild(loader);
  15.            
  16.             //Add the event handlers
  17.             loader.contentLoaderInfo.addEventListener(Event.OPEN, handleOpen);
  18.             loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, handleProgress);
  19.             loader.contentLoaderInfo.addEventListener(Event.COMPLETE, handleComplete);
  20.            
  21.             //Load the external image
  22.             loader.load( new URLRequest("image.jpg"))
  23.         }
  24.        
  25.         private function handleOpen (event:Event):void {
  26.             trace("open");
  27.             loaderStatus = new TextField();
  28.             addChild(loaderStatus);
  29.            
  30.             loaderStatus.x = 100;
  31.             loaderStatus.y = 100;
  32.             loaderStatus.text = "Loading: 0%";
  33.            
  34.         }
  35.        
  36.         private function handleProgress (event:ProgressEvent):void {
  37.             var percent:Number = event.bytesLoaded/event.bytesTotal * 100;
  38.             loaderStatus.text = "Loading:" + percent + "%";
  39.             loaderStatus.autoSize = TextFieldAutoSize.LEFT;
  40.             trace(percent)
  41.         }
  42.        
  43.         private function handleComplete(event:Event):void {
  44.             trace("complete");
  45.             removeChild(loaderStatus);
  46.             loaderStatus = null;
  47.         }
  48.     }
  49. }

To Instantiate this code, add the following code to the first frame of your flash file and make sure the class is saved in the same directory as the flash file.

Actionscript:
  1. import ImageLoader;
  2.  
  3. var images:LoaderExample = new LoaderExample();
  4. addChild(images);

Share and Enjoy:
  • BlinkList
  • del.icio.us
  • digg
  • Furl
  • Ma.gnolia
  • Simpy
  • Slashdot
  • Spurl
  • StumbleUpon
  • Technorati

1 Comment »

  1. Red Said,

    June 4, 2008 @ 3:12 pm

    Nice work Jake. Clean and concise.

RSS feed for comments on this post

Leave a Comment