Loading External Resources

Loading images

There is a Loader object which allows you to load, and then display images and SWF files. It will be responsible for down loading the file, and letting Flash know it's progress and when the download is complete.

var myLoader:Loader = new Loader(); 

The Loader object takes a URLRequest object. It is responsible for determining which file should be downloaded.

var fileRequest:URLRequest = new URLRequest("myImage.jpg"); // this can also be SWF
myLoader.load(fileRequest);

You will want to listen for the Complete Event as a minimum. If you are potentially loading large graphics, you will want to listen for the progress event as well most likely, so you can build preloaders for your sub content.

myLoader.contentLoaderInfo.addEventListener
		(ProgressEvent.PROGRESS, onLoaderProgress);
myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaderReady); 

The onLoaderReady method is needed, so you can do something with the downloaded file.

public function onLoaderReady(e:Event) {
	// the image is now loaded
	addChild(myLoader);
}

A progress loader can be developed with the onLoaderProgress event handler. This is not required, but can be recommended to have.

public function onLoaderProgress(e:ProgressEvent) {
	// this is where progress can be determined
	trace(e.bytesLoaded, e.bytesTotal);
}