Load-error handling : Loader « Network « Flash / Flex / ActionScript






Load-error handling

 
package {
  import flash.display.*;
  import flash.net.URLRequest;
  import flash.events.*
  import flash.text.*;

  public class Main extends Sprite {
    private var loader:Loader;
    private var progressOutput:TextField;

    public function Main(  ) {
      createLoader(  );
      createProgressIndicator(  );
      load(new URLRequest("sunset.jpg"));
    }

    private function createLoader (  ):void {
      loader = new Loader(  );
      loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,
                                                progressListener);
      loader.contentLoaderInfo.addEventListener(Event.COMPLETE,
                                                completeListener);
      loader.contentLoaderInfo.addEventListener(Event.INIT,
                                                initListener);
      loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,
                                                ioErrorListener);
    }

    private function createProgressIndicator (  ):void {
      progressOutput  = new TextField(  );
      progressOutput.autoSize   = TextFieldAutoSize.LEFT;
      progressOutput.border     = true;
      progressOutput.background = true;
      progressOutput.selectable = false;
      progressOutput.text       = "LOADING...";
    }

    private function load (urlRequest:URLRequest):void {
      loader.load(urlRequest);
      if (!contains(progressOutput)) {
        addChild(progressOutput);
      }
    }

    private function progressListener (e:ProgressEvent):void {
      progressOutput.text = "LOADING: "
                          + Math.floor(e.bytesLoaded / 1024)
                          + "/" + Math.floor(e.bytesTotal / 1024) + " KB";
    }

    private function initListener (e:Event):void {
      addChild(loader.content);
    }

    private function completeListener (e:Event):void {
      removeChild(progressOutput);
    }

    private function ioErrorListener (e:IOErrorEvent):void {
      progressOutput.text = "LOAD ERROR";
    }
  }
}

        








Related examples in the same category

1.Main.Swf Listener Receives Notification for an Event Targeted at a Module.swf Display Object
2.Add mouse click listener to a loaded swf
3.Loading External Images at Runtime
4.Add event listener to content loader info
5.Loading and Interacting with External Movies
6.Adding a Loader to the display list
7.Displaying load progress
8.Polling for the existence of a loaded object
9.Handling an event announcing a loaded object's availability
10.Load an image using Loader