Tag Archives: loader

ActionScript Flash

Loader vs URLLoader

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:

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

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