Load media, datas file, or variables

Use of events listeners loading files

Flash API permit us to add events listeners on object. For example you can control the loading and detect errors.

Use flash.events.IOErrorEvent if you want to catch errors. If an error occured during the loading, a defined function will be executed and will permit us to get some details about it.

We also can use these events handlers : for the end of a loading Event.COMPLETE and the progress, ProgessEvent.PROGRESS

package;
import flash.Lib;
import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.IOErrorEvent;
import flash.events.Event;
import flash.events.ProgressEvent;
      class Main
      {
      		public var chargeur:Loader;
      		static function main()
      		{
      			new Main();
      		}
      		public function new()
      		{      		 
      			exemple2();
      		} 
      		public function exemple2()
      		{
      			chargeur = new Loader();
      			chargeur.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, displayError);
      			chargeur.contentLoaderInfo.addEventListener(Event.COMPLETE, finished);
      			chargeur.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progress);
      			chargeur.load(new URLRequest('photo.jpg'));      			
      			
      			trace("Loading");
      		}
      		public function displayError(e:IOErrorEvent)
      		{
      			trace("An error occured.");
      		}
      		public function finished(e:Event)
      		{
      			Lib.current.addChild(loader);
      			trace("Loading is over");
      		}
      		public function progress(e:ProgressEvent)
      		{
      			trace(Std.int(e.currentTarget.bytesLoaded/e.currentTarget.bytesTotal)*100+" %");
      		}
      }

Just notice that events must not be defined directly on the Loader object, but on its property contentLoaderInfo. (return LoaderInfo)

Result :

output

Alternative content

Get Adobe Flash player

(97 times)