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
[as]
package {
import flash.display.*;
import flash.text.*;
import flash.net.URLRequest;
import flash.events.*;

public class LoaderExample extends Sprite {

public var loaderStatus:TextField;

public function LoaderExample() {
//Create the Loader and add it to the display list
var loader:Loader = new Loader();
addChild(loader);

//Add the event handlers
loader.contentLoaderInfo.addEventListener(Event.OPEN, handleOpen);
loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, handleProgress);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, handleComplete);

//Load the external image
loader.load( new URLRequest(“image.jpg”))
}

private function handleOpen (event:Event):void {
trace(“open”);
loaderStatus = new TextField();
addChild(loaderStatus);

loaderStatus.x = 100;
loaderStatus.y = 100;
loaderStatus.text = “Loading: 0%”;

}

private function handleProgress (event:ProgressEvent):void {
var percent:Number = event.bytesLoaded/event.bytesTotal * 100;
loaderStatus.text = “Loading:” + percent + “%”;
loaderStatus.autoSize = TextFieldAutoSize.LEFT;
trace(percent);
}

private function handleComplete(event:Event):void {
trace(“complete”);
removeChild(loaderStatus);
loaderStatus = null;
}
}
}
[/as]

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.

[as]
import ImageLoader;

var images:LoaderExample = new LoaderExample();
addChild(images);
[/as]

Categories: ActionScript 3.0Flash

2 thoughts on “Part 2: Using LoaderInfo Events to create reusable ImageLoader Class”

Red · June 4, 2008 at 3:12 pm

Nice work Jake. Clean and concise.

santosh chandan · January 19, 2011 at 5:54 am

thank u very much..

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

Design

The Best of Web Conferences 2008

I have compiled a list of the most popular flash and web conferences that are offered around the world. Flash: Flash on the Beach Flash Forward Flash in the Can Flash Forum Web Design/Creative: Future Read more…

ActionScript 3.0

Common ActionScript 3 Errors and Explanations

So as with the new version of ActionScript there is a new level of complexity when it comes to creating ActionScript, but there seems to be better debugging too. The only barrier I have hit Read more…

ActionScript 3.0

Part 1: ActionScript 3.0 LoaderInfo Useful Events

Im very impressed with ActionScript 3.0 and how all the events work. I have been reading up lately on progress events specifically the open, progress, and complete events. You can manipulate these events in so Read more…