AS3 Basics: How to load an external image file using Actionscript 3

Hey all. It’s been a long time since i’ve written. hopefully i’ll be at least bringing you something each week from now on, instead of say when i feel like :P

Anyways…

Heres how to load an external file into an SWF.

var myLoader:Loader = new Loader(); // create a new instance of the 'Loader' class
var myURLRequest:URLRequest = new URLRequest("image.jpg"); // create a instance of the 'URLRequest' class and pass it our URL to our image
myLoader.load(myURLRequest); // use the 'Loader' class's 'load' method to load our URL request, and load our image into flash

myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, myLoader_COMPLETE); // add an event to run on successful load
function myLoader_COMPLETE(e:Event)
{
	addChild(myLoader); // add the 'Loader' to the display list to display they image on stage
}

myLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, myLoader_IO_ERROR); // add an event to run on error
function myLoader_IO_ERROR(e:IOErrorEvent)
{
	trace("Thar be a breach in ye hull! Arr!");
}

Woo and thats it!

Download the Source movie below:

Subscribe

Subscribe to our e-mail newsletter to receive updates.