HOWTO: Using Loaders in AS3
What are these ominous Loaders is what people ask when they first start into AS3? Loaders help to facilitate the loading of sprites or content that aren’t in the DisplayObjectList tree just yet. Loaders allow you to load all types of files swf, jpg, png, etc and put them right in the display list. A very simple basic load is below to describe the basics of loading content. This can be used many ways within individual classes or shared and add in multiple file downloading. You might add zip file downloading and then automatic upzipping of assets to load in. There are many ways to architect your downloading and preloading but this is the basics of a loader in AS3.
var request:URLRequest = new URLRequest("content.swf"); var loader:Loader = new Loader(); loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loadProgress); loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete); function loadProgress(event:ProgressEvent):void { var percentLoaded:Number = event.bytesLoaded/event.bytesTotal; percentLoaded = Math.round(percentLoaded * 100); trace("Loading: "+percentLoaded+"%"); } function loadComplete(event:Event):void { trace("Complete"); } loader.load(request); addChild(loader);
All DisplayObject instances have a loaderInfo property consisting of a LoaderInfo instance that provides information about that object’s loaded content (if applicable). The Loader instance contains this property along with an additional contentLoaderInfo property which represents the LoaderInfo instance of the content being loaded. When loading content into a Loader, the contentLoaderInfo property is the object you would want to add listeners to for information about the loaded content such as when new bytes are being loaded into the player (for a preloader) and when loading has completed.
The events associated with LoaderInfo instances includes:
- complete:Event — Dispatched by the associated LoaderInfo object when the file has completed loading. The complete event is always dispatched after the init event.
- httpStatus:HTTPStatusEvent — Dispatched by the associated LoaderInfo object when a network request is made over HTTP and Flash Player can detect the HTTP status code.
- init:Event — Dispatched by the associated LoaderInfo object when the properties and methods of the loaded SWF file are accessible. The init event always precedes the complete event.
- ioError:IOErrorEvent — Dispatched by the associated LoaderInfo object when an input or output error occurs that causes a load operation to fail.
- open:Event — Dispatched by the associated LoaderInfo object when the loading operation starts.
- progress:ProgressEvent — Dispatched by the associated LoaderInfo object as data is received while load operation progresses.
- unload:Event — Dispatched by the associated LoaderInfo object when a loaded object is removed.
[source]
