AS3: A URLLoader Class
Monday, May 3rd, 2010I’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:
import com.grasshopper.utils.gpUrlLoader; //or import com.grasshopper.utils.*; |
Loading an Image (or SWF)
To load an image:
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.
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.
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
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
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
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)
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).
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:
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.



June 18th, 2010 at 10:43 pm
[...] Part 1, I have already discussed one of the classes of the gpAS3Libary in a previous post (see: A UrlLoader Class). In this post, I will discuss the gpConfig [...]
January 7th, 2011 at 9:33 am
Great Loader class, however i am having one problem with it. It creates a urlLoader or a Loader depending on what it is u are loading. I’m loading a jpg image which creates a Loader. There is then no way to track its progress with “trace(ldr.urlLoader.bytesLoaded);” as in your example, because the Loader doesnt have a bytesLoaded property on it, only the URL Loader. Other than that it tracks the progress of XML and text files just fine.