Checking a ScrollPane’s download progress (or lack thereof)

Somebody emailed me recently about this topic, so I came up with a quick and dirty solution. Not sure if there’s a better way, but thought that somebody out there may find this useful, so here it is:

1. Drag a ScrollPane component onto your Stage and give it an instance name of my_scrollPane.

2. Add the following ActionScript to frame 1 of the main timeline. (Watch for wrapping lines!)

my_scrollPane.addEventListener(“complete”, myLoaderCompleteListener);
function myLoaderCompleteListener(eventObj:Object):Void {
if (eventObj.target.getBytesTotal() == -1) {
eventObj.target.visible = false;
var t:TextField = eventObj.target._parent.createTextField(“error_txt”, 10, eventObj.target.x, eventObj.target.y, 100, 20);
t.autoSize = “left”;
t.text = “unable to load file”;
return;
}
// continue setting up.
}
my_scrollPane.contentPath = “http://www.deseloper.com/thisImageFileDoesntExist.jpg”;

3. Save and test.

Because the image file doesn’t exist, the complete event is dispatched to the ScrollPane instance and getBytesTotal() returns -1. The complete listener checks that the image was successfully loaded. If not, the ScrollPane instance is hidden and a text field is created and displays an error message.

Voila.