URLLoader and Loader classes

Have you ever had mystery errors that your content stopped working without a reason? Possibly you haven’t registered all important listeners, most examples are simplified, i.e.

var image:Loader = new Loader();
image.addeventListener(Event.COMPLETE, onImageLoaded);
image.load(new URLRequest("myImage.jpg"));


and this is fine as long:
– file exist
– your flash can access this resource
– you have debug version of flash player (it doesn’t stop error but it throws them in your face:) )

basically you should get a habit of listening for ALL important events: IOErrorEvent.IO_ERROR and SecurityErrorEvent.SECURITY_ERROR, those two are really important and will save you a lot of trouble. So revised example will look like follows:

var image:Loader = new Loader();
image.addeventListener(IOErrorEvent.IO_ERROR, onImageError, false, 0, true);
image.addeventListener(SecurityErrorEvent.SECURITY_ERROR, onImageError, false, 0, true);
image.addeventListener(Event.COMPLETE, onImageLoaded, false, 0, true);
image.load(new URLRequest("myImage.jpg"));

//somwhere later
function onImageError(e:Event):void
{
    //do something with the error, log it handle it (fall-back image) etc.
}

happy coding
p.s. title says about Loader and URLLoader as most frequently used but it is applicable to any Class that access external resource.

This entry was posted in actionscript, flash and tagged , , . Bookmark the permalink.