ActionScript 3 Loading External Files Loading External Images/SWFs With The Loader


  1. Create a Loader object:

    var loader:Loader = new Loader();   //import 
  2. 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
  3. Load the desired file:

     loader.load(new URLRequest("image.png"));
  4. 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
         //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 the Event.COMPLETE event.

imports needed:

import flash.display.Loader;