AS3: A URLLoader Class

I’m working on a Flash/AS3 application where I am loading various types of files (images, xml, etc). After writing the same code over and over again, I decided to create an AS3 class to use with my application. This class will load image, swf, text, xml, html, stylesheet, json, and sound files.

gpUrlLoader Class

The gpUrlLoader Class is part of my AS3 Library (gpAS3Library) that I store on GitHub. The class package is set up under com/grasshopper/utils/. Included in the library is Adobe’s Json classes (part of adobe’s as3corelib). The Json classes are needed for Json serialization (encode, decode, etc).

To include the gpUrlLoader package:

?View Code ACTIONSCRIPT
import com.grasshopper.utils.gpUrlLoader;
//or
import com.grasshopper.utils.*;

Loading an Image (or SWF)

To load an image:

?View Code ACTIONSCRIPT
import flash.display.Loader;
import flash.events.Event;
import com.grasshopper.utils.*;
 
var url:String = 'my_image.jpg';
var ldr:gpUrlLoader = new gpUrlLoader();
ldr.addEventListener(Event.COMPLETE, imageLoaded);
ldr.init(url, 'image');
 
// for swf use:
// ldr.init(url, 'swf');
 
function imageLoaded(event:Event):void {
	var img:Loader = ldr.getImage();
        // for swf use:
        // var img:Loader = ldr.getSWF();
 
	addChild(img);
}

After instantiating the gpUrlLoader class, I called the init method while passing it the load type (image). I set up an event listener so that the main application will know when the image was loaded. Simple enough.

You can also get the image width and height.

?View Code ACTIONSCRIPT
function imageLoaded(event:Event):void {
	var img:Loader = ldr.getImage();
 	addChild(img);
        var w:Number = img.width;
        var h:Number = img.height;
}

Loading and XML file

Loading the xml file is the same process as loading an image.

?View Code ACTIONSCRIPT
import flash.events.Event;
import com.grasshopper.utils.*;
 
var url:String = 'my_xml.xml';
var ldr:gpUrlLoader = new gpUrlLoader();
ldr.addEventListener(Event.COMPLETE, xmlLoaded);
ldr.init(url, 'xml');
 
function xmlLoaded(event:Event):void {
	var xmlData:XML = ldr.getXML();
}

Loading JSON file

?View Code ACTIONSCRIPT
import flash.events.Event;
import com.grasshopper.utils.*;
 
var url:String = 'my_json.js';
var ldr:gpUrlLoader = new gpUrlLoader();
ldr.addEventListener(Event.COMPLETE, jsonLoaded);
ldr.init(url, 'json');
 
function jsonLoaded(event:Event):void {
	var json:Object = ldr.getJson();
}

Note: To use the Json loader, you must compile your application using Adobe’s Json classes (I’ve included the zipped classes as part of the gpAS3Libary).

Loading A Stylesheet

?View Code ACTIONSCRIPT
import flash.events.Event;
import flash.text.StyleSheet;
import com.grasshopper.utils.*;
 
var url:String = 'styles.css';
var ldr:gpUrlLoader = new gpUrlLoader();
ldr.addEventListener(Event.COMPLETE, sheetLoaded);
ldr.init(url, 'stylesheet');
 
function sheetLoaded(event:Event):void {
	var sheet:Stylesheet = ldr.getStyleSheet();
}

Loading A Sound File

?View Code ACTIONSCRIPT
import flash.events.Event;
import com.grasshopper.utils.*;
 
var url:String = 'my_sound.mp3';
var ldr:gpUrlLoader = new gpUrlLoader();
ldr.addEventListener(Event.COMPLETE, soundLoaded);
ldr.init(url, 'sound');
 
function soundLoaded(event:Event):void {
	var snd:Sound = ldr.getSound();
        snd.play();
}

Loading A Text File (to include, HTML)

?View Code ACTIONSCRIPT
import flash.events.Event;
import com.grasshopper.utils.*;
 
var url:String = 'my_text.html';
var ldr:gpUrlLoader = new gpUrlLoader();
ldr.addEventListener(Event.COMPLETE, textLoaded);
ldr.init(url, 'text');
 
function textLoaded(event:Event):void {
	var txt:String = ldr.getText();
}

Other Event Listeners

In addition to the Event.COMPLETE event, you can also listen to Event.PROGRESS and LOADERROR. Actually LOADERROR is a custom event that I created for the gpUrlLoader class (gpUrlLoader.LOADERROR).

?View Code ACTIONSCRIPT
import flash.display.Loader;
import flash.events.Event;
import flash.events.IOErrorEvent;
import com.grasshopper.utils.*;
 
var url:String = 'my_image.jpg';
var ldr:gpUrlLoader = new gpUrlLoader();
ldr.addEventListener(Event.COMPLETE, imageLoaded);
ldr.addEventListener(ProgressEvent.PROGRESS, onProgress);
ldr.addEventListener(gpUrlLoader.LOADERROR, ioError);
ldr.init(url, 'image');
 
function onProgress(event:Event):void {
	trace(ldr.urlLoader.bytesLoaded);
}
 
function imageLoaded(event:Event):void {
	var img:Loader = ldr.getImage();
	addChild(img);
}
 
function ioError(e:Event):void {
	trace('Error loading file');
}

Notice that in order to track the progress you must use the urlLoader class variable:

?View Code ACTIONSCRIPT
function onProgress(event:Event):void {
	trace(ldr.urlLoader.bytesLoaded);
}

That’s it. You can examples of the gpUrlLoader on my demo section. Again, the gpUrlLoader can be downloaded from GitHub (gpAS3Library). Enjoy.

Be Sociable, Share!

Checkout My New Site - T-shirts For Geeks