Page 1 of 1

Differences in AS3 Loader Classes Rate Topic: ***** 1 Votes

#1 thehat   User is offline

  • awake ? web();
  • member icon

Reputation: 106
  • View blog
  • Posts: 951
  • Joined: 28-February 08

Posted 30 October 2009 - 05:35 AM

In this short tutorial I will provide an overview of two different loader classes in AS3, Loader and URLLoader. The basic difference between these is that the Loader class is suitable for loading objects to be displayed, and the URLLoader is used for loading data. This is immediately apparent if you take the time to read both class definitions in the language reference, as the properties of the two classes are dramatically different.

First of all let's look at the Loader class. It has only two public uninherited properties, content and contentLoaderInfo. The content property is a DisplayObject, which means that you can attach it to a DisplayObjectContainer. This is very important when you're trying to load an image of swf and place it on the stage! The other property, contentLoaderInfo, is a LoaderInfo object that contains information about the file you're trying to load. It is this that you will use to find the bytesLoaded and bytesTotal, and to attach listeners to the Loader. Here's a simple example:

//create new Loader object
var myLoader = new Loader();
//set up listeners for progress and complete
//trigger the load
myLoader.load(new URLRequest("my_image.gif"));

function onloadProgress(evt:ProgressEvent):void {
	//find the loaded percentage
	var perc:Number = evt.bytesLoaded/(evt.bytesTotal/100);
	trace("Loaded Percent: " + perc);

function onloadComplete(evt:Event):void {
	//clean up the listeners,onloadProgress);,onloadComplete);
	//add the image to the display

So what about the URLLoader? As the documentation so eloquently puts it: "The URLLoader class downloads data from a URL as text, binary data, or URL-encoded variables. Its properties are focused on the delivery of data. Aside from the self explanatory bytesLoaded and bytesTotal, it has only two uninherited properties, data and dataFormat. The property dataFormat is a simple string that tells us what type of information the data property holds. This is important because the data property is a * type. The * data type is new to ActionScript, and indicates that the property can potentially be one of many different data types, in this case String, ByteArray or URLVariables. For the most part this will be text. XML files will load in as text and then parsed by an XML object like so:

function dataLoadComplete(evt:Event):void {
	try {
		myXMLData = new XML(;
	} catch (e:TypeError){
		//Could not convert the data
		//maybe because is not formated correctly
		trace("Could not parse the XML: " + e.message);

Likewise, when sending data from a server script the format will usually be text.  You might send something from php like this:
echo "myVar1=value1&myVar2=value2&myVar3=value3";

When the load is complete you would have to use the URLVariables class to convert your data into a usable object:
function dataLoadComplete(evt:Event):void {
	var vars:URLVariables = new URLVariables(;
	trace(vars.myVar1); //traces value1

So there you have it. Just remember, Loader for display, URLLoader for Data.

Is This A Good Question/Topic? 0
  • +

Page 1 of 1