1. General loading principles
Flash can load data as big pieces of string. a list of name/value pairs. What I mean? That data must be a named variable with some value, like this:
name=Jorge
Here I have a variable named "name" with a value "Jorge". But what happens if I need more info, perhaps last name and age? I can use a string like this:
name=Jorge&lastname=Solis&age=30
Now I have three variables, each one holding a different information. Note that I use the "&" sign to separate values. This way I can pass any quantity of data to Flash, each one in a name/value pair format. This info could be in any file as far as it can be loaded by our movie.
Now let's move to the Flash side. In AS3 you should use the URLLoader class in conjunction with URLRequest and URLLoaderDataFormat.VARIABLES as the dataFormat to read URL-encoded data.
So, having name=Jorge in a text file named data.txt, I can use this:
//Create the URLLOader instance
var myLoader:URLLoader = new URLLoader()
//the data will come as URL-encoded variables
myLoader.dataFormat = URLLoaderDataFormat.VARIABLES
//Load using an URLRequest, even beeing local
myLoader.load(new URLRequest("files/txt/anastasio.txt"))
//add a listener for the complete event
myLoader.addEventListener(Event.COMPLETE, onLoad)
function onLoad(ev:Event){
trace ("Data loaded")
}
So here we first instantiate a URLLoader object, then we use the load method to load the content of the text file using a URLRequest object, and finally we add a listener to the complete event that fires the onLoad function when the content is totally loaded. Note that we type all variables so the compiler can check it. Even beeing longer code than AS2, we have some benefits:
- Specialized classes handle each task, so the performance is better
- Data is encapsulated into the URLLoader.data property, so it doesn't overwrite
any other variable in our movie
- The event.complete event let us know when downloading finishes
Other events
There are a couple of events that helps following the data download:
open: Dispatched when the download operation commences following a
call to the URLLoader.load() method.
progress: Dispatched when data is received as the download operation progresses.
The second one is great news from those wondering how to monitor the download of big pieces of data, since the old LoadVars object doesn't have this property. The progress method broadcast an event with getBytesLoaded and getBytesTotal properties that can be used as in any other preloader, so you can show visually the download proccess. Anyway, take into account that if you load little pices of data (as our next example) monitoring the download make no sense
Error handling
But what happens if for some reason the download fails? There are some improvements also in error handling, since the old LoadVars in AS2 just return a boolean value for success or not, but is up to you to figure out what happens. The URLLoader adds some error events sumarize here:
httpStatus : An HTTPStatusEvent object does not necessarily indicate an error condition; it simply reflects the HTTP status code (if any) that is provided by the networking stack. Some Flash Player environments may be unable to detect HTTP status codes; a status code of 0 is always reported in these cases.
ioError: dispatched if a call to URLLoader.load() results in a fatal error
that terminates the download.
securityError: Dispatched if a call to URLLoader.load() attempts to load data
from a server outside the security sandbox.
To monitor this events, you should add a listener and a callback for each one
Examples
So we have seen how Flash reads name/value pairs and how to use a URLLoader object to load it. Use the examples files from the source in this tutorial to follow next chapters. Each method is inside his own folder (txt, XML, DB and remoting)
Now move to our example using a text file, putting all concepts togheter so
you can get the whole picture.

5 most recent
Flash streaming servers
Tree menu
Flash Spell Checker
Flash Remoting Library
MX 2004 Chart/Poll
Articles