Create a Loader object:
var loader:Loader = new Loader(); //import
Add listeners on the loader. Standard ones are complete and io/security errors
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete); //when the loader is done loading, call this function
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, loadIOError); //if the file isn't found
loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR, loadSecurityError); //if the file isn't allowed to be loaded
Load the desired file:
loader.load(new URLRequest("image.png"));
Create Your Event Handlers:
function loadComplete(e:Event):void {
//load complete
//the loader is actually a display object itself, so you can just add it to the display list
addChild(loader)
//or addChild(loader.content) to add the root content of what was loaded;
}
function loadIOError(e:IOErrorEvent):void {
//the file failed to load,
}
function loadSecurityError(e:SecurityError):void {
//the file wasn't allowed to load
}
Loading with the Loader class is asynchronous. This means after you call
loader.load
the application will continue running while the file loads. Your loader content isn't available until the loader dispatches theEvent.COMPLETE
event.
imports needed:
import flash.display.Loader;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.SecurityErrorEvent;
import flash.net.URLRequest;