Warning: Trying to access array offset on value of type bool in /var/www/vhosts/tomelliott.com/httpdocs/wp-content/themes/tomelliott/single.php on line 12

AS3 detect\catch invalid loader url request

17 May, 2011 by Tom Elliott

I like AS3’s ability to find and track all sorts of errors and coding bugs but one minor issue I had recently was when trying to detect and catch an invalid url request for dynamically loaded images.

At first the solution seemed obvious – I thought using a simple try {} catch {} method for the IOErrorEvent would be enough as indicated below:
var imgLoader:Loader = new Loader(); // Create a new loader object
try {
imgLoader.load(new URLRequest("filename.jpg"));
addChild(imgLoader);
} catch(e:IOErrorEvent){ {
trace("Error loading image thumbnail");
//Perform desired catch action
}

The solution in fact (and this may only apply to images and certain types of file) was to use an event listener for the content of the Loader object:

var imgLoader:Loader = new Loader();
imgLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, catchBadURL);
imgLoader.load(new URLRequest("filename.jpg"));
addChild(imgLoader);
function catchBadURL (e:IOErrorEvent) {
trace("Error loading image thumbnail");
//Perform desired catch action
}

Hopefully this will save you the headache it gave me!