hide side navigation
    5 most recent
    Web Services
    Library's
    Component's
    Applications
    Articles
  Flash button as Flex icon  Flex form by email  Hello Remoting with AS3  AS3 Saving data from Flash  AS3 Loading data into Flash  Fire Effect  Contact form  Dragable buttons  Hello World with openamf  Loading helper classes  Upload with Flash 8  Transitions effects  Snapshot with Flash 8  Hello World Remoting AS2  Flash AS2 Remoting Connector  Saving data from Flash  Loading data into Flash  FlashCom & Remoting login  Cell Renderer API  Editing a table using remoting components  Flash MX2004 web service classes  Browsing a catalog  amfphp Documentation  Hello World Remoting  Online Store with AMFPHP  Flash clients for Web Services  Web Service Walk Though with NuSoap  Popup windows in flash with javascript  Installing Apache/PHP  MoreOver News Feeds  Load Edit Save Text Files via CGI  Save Movie Clip Postion via PHP and MySQL
Current Page (1) Next Page >>  View Article Example >> 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8

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.COMPLETEonLoad)
    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.

Current Page (1) Next Page >> 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8