Posts Tagged ‘AS3 Loader URLLoader’

Loader vs URLLoader

Friday, October 30th, 2009

Today I tried to load an image, and I failed.  I failed because I was trying to use the URLLoader class.  As I discovered this won’t work, because although it will perform a load and happily dispatch a complete event, it doesn’t have the facility to display anything.

What you need for displaying is the Loader class, or more specifically it’s content property, which is a DisplayObject.  Like so:


//create new Loader object
var myLoader = new Loader();
//set up a listener
myLoader.contentLoaderInfo.addEventListener(
Event.COMPLETE,loadComplete);
//trigger the load
myLoader.load(new URLRequest("my_image.gif"));
//
function loadComplete(evt:Event):void {
//clean up the listener
evt.target.contentLoaderInfo.removeEventListener(
Event.COMPLETE,loadComplete);
//add the image to the display
addChild(evt.target.content);
}

So basically the difference is this: Loader for display objects, URLLoader for data connections.