Home arrow Flash arrow Page 2 - The Power of LoadVars Object
FLASH

The Power of LoadVars Object


The LoadVars object offers a cleaner, easier way to carry out the task of exchanging data with a server. Adi Reddy Mora explains how this object, introduced in Flash Player 6, can help you to more easily transfer variables between a Flash movie and a server.

Author Info:
By: Adi Reddy Mora
Rating: 4 stars4 stars4 stars4 stars4 stars / 31
February 09, 2005
TABLE OF CONTENTS:
  1. · The Power of LoadVars Object
  2. · Advantages of the LoadVars class
  3. · Receiving the variables in server-side script
  4. · Using LoadVars Object for loading data from text files
  5. · LoadVars.decode

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
The Power of LoadVars Object - Advantages of the LoadVars class
(Page 2 of 5 )

The advantages of the LoadVars class include the following:

  • You don't need to create container movie clips for holding data or clutter existing movie clips with variables specific to client/server communication.

  • The class interface is similar to the XML object, which provides some consistency in ActionScript. It uses the methods load(), send(), and sendAndLoad() to initiate communication with a server. The main difference between the LoadVars and XML classes is that the LoadVars data is a property of the LoadVars object, rather than an XML Document Object Model (DOM) tree stored in the XML object.

  • The class interface is more straightforward—with methods named load, send, sendAndLoad—than the older loadVariables interface.

  • You can get additional information about the communication, using the getBytesLoaded and getBytesTotal methods.

  • You can get progress information about the download of your data.

  • The callback interface is through ActionScript methods ( onLoad) instead of the obsolete, deprecated onClipEvent (data) approach is required for loadVariables.

  • There are error notifications.

  • You can add custom HTTP request headers.

The following are the five basic steps involved in using LoadVars Object for communicating with any server:

  1. Instantiate two LoadVars objects, one for sending variables from flash and one to receive any variables that are sent from the server.

  2. Define and associate the flash variables to LoadVars object that are required to send to the server.

  3. Use sendAndLoad method to send the associated variables to the server as POST/Get variables and also specify the LoadVars object for receiving variables from the server.

  4. Use onLoad event handler method to assign a function which fires when the variables are received from the server. The onLoad handler is passed a true or false Boolean value indicating whether the data was submitted successfully.

  5. Create a function with the name specified in step 4 to handle the received variables. The function will be called when the server's reply has been received. You need to write this function yourself.

Let us see an example now to demonstrate the above steps in ActionScript 2.0

Step 1

var reply_lv = new LoadVars();

var send_lv = new LoadVars();

Step 2

send_lv.type = "BUTTON";

send_lv.id = "1";

Step 3

send_lv.sendAndLoad("asp_net_php_page", reply_lv, "POST");

Step 4

reply_lv.onLoad = loadedDotNetVars;

Step 5

function loadedDotNetVars(success){

    if(success) {

          // operation was a success

          trace(reply_lv.name)

    }

    else {

          // operation failed

    }
}

If the sendAndLoad operation finishes without errors, any variables returned by the server will be placed into reply_lv and the loadedDotNet function will be invoked.

Note: "name" is the variable name that was specified in the server-side script.

If the http send method was set to GET, all the variables will be sent to the server in query string format like:

 

type=BUTTON&id=1

Suppose you are sending arrays to the server, and those array values are sent as a comma-delimited list. For example, if you are sending the following data:

send_lv.type = "BUTTON";

send_lv.id = "1";

send_lv.colors = [ "red", "white", "blue" ];

Then the query string will look like this:

type= BUTTON&id=1&colors=red,white,blue

 


blog comments powered by Disqus
FLASH ARTICLES

- More Top Flash Game Tutorials
- Top Flash Game Tutorials
- Best Flash Photo Gallery Tutorials
- The Top Flash Tutorials for Menus
- 7 Great Flash Tutorials
- Adobe Creative Suite 5.5 Now Available
- Critical Flash Vulnerability Heats Up the Web
- More on Nonpersistent Client-Side Remote Sha...
- Nonpersistent Client-Side Remote Shared Obje...
- Using the Decorator Pattern for a Real Web S...
- Using Concrete Decorator Classes
- Delving More Deeply into the Decorator Patte...
- The Decorator Pattern in Action
- A Simple Decorator Pattern Example
- Decorator Pattern

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 7 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials