The Power of LoadVars Object
by Adi Reddy Mora
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.
The LoadVars object was introduced in Flash Player 6 to provide a cleaner, much more object-oriented interface for the common task of exchanging data with a server.
It is an alternative to the loadVariables action with more flexibility for transferring variables between a Flash movie and a server.*
*Here after "server" refers to any of the server-side scripting technology (ASP/ASP.NET/PHP/JSP/ColdFusion/etc)
You can use the LoadVars object to obtain verification of successful data loading, progress indications, and stream data while it downloads. The LoadVars object works much like the XML object; it uses the methods load, send, and sendAndLoad to communicate with a server. The main difference between the LoadVars object and the XML object is that LoadVars transfers ActionScript name and value pairs, rather than an XML DOM tree stored in the XML object.
It also lets you send only the variables that are required by the server instead of sending all the variables, as in the case of loadVariables or loadVariablesNum.
The LoadVars object is supported by Flash Player 6 and later.
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
[code]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
}
}[/code]
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:
[code] type=BUTTON&id=1[/code]
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:
[code] send_lv.type = "BUTTON";
send_lv.id = "1";
send_lv.colors = [ "red", "white", "blue" ];[/code]
Then the query string will look like this:
[code] type= BUTTON&id=1&colors=red,white,blue[/code]
You can access the flash variables in the server-side script normally, in the same way you access form POST/GET variables. The variable names will be the names you use when associating the variables to the LoadVars Object.
[code]Example 1 (ASP): Request(“ type”);
Example 2 (PHP): $_POST[\'type\']
Example 3 (ColdFusion): #FORM. type# [/code]
Sending variables back to flash from server:
You can also send variables back to flash from the server. You just need to print the data on the server-side page for flash to read that data in the following format:
[code]&name=John&id=342&ecode=e123[/code]
In the above string name, id and ecode are the variable names that can be used in flash to read the data. You can pass as many variables as you require by seperating them with ampersand (&) symbol.
Example 1 (ASP):
You can use Response object to write the data in ASP
[code]Response.Write("&name=John&id=342&ecode=e123")[/code]
Example 2 (PHP):
You can use print or echo language constructs to write data in PHP.
[code]echo "&name=John&id=342&ecode=e123";[/code]
Example 3 (ColdFusion):
You can use [code]<cfoutput>[/code] tag to output data to flash.
[code]<cfoutput>&name=John&id=342&ecode=e123</cfoutput>[/code]
The LoadVars class consists of three methods for communicating with the server.
* LoadVars.load
* LoadVars.send
* LoadVars.sendAndLoad
We have seen the usage of LoadVars.sendAndLoad method for sending and receiving data to and fro from the server.
Similarly, based on your requirements, if you want to only receive the data from the server, you can use LoadVars.load method with the parameter URL to load data from the server.
[code]LoadVars.load("asp_net_php_page");[/code]
If you want to only send data to the server you can use the LoadVars.send method.
[code] LoadVars.load("asp_net_php_page", "_blank", "POST");[/code]
LoadVars Object can also be used to load data from text files.
The following are the four basic steps involved in using LoadVars Object for loading text from .txt files into flash:
1. Instantiate a LoadVars object.
2. Use load method to load the text file data into LoadVars object.
3. Use onload event handler method to assign a function which fires when the variables are received from the text file.
4. Create a function with the name specified in step 3 to handle the received variables
Let us see an example now to demonstrate the above steps in ActionScript 2.0
[code]Step 1
var load_lv = new LoadVars();
Step 2
load_lv.load("textfile.txt");
Step 3
load_lv.onload = loadTextVariables;
Step 4
function loadTextVariables(success){
if(success){
trace(this.name);
}
}
The text file should contain the data in the following format:
&name=John&id=342&ecode=e123[/code]
The following tables list all methods, properties and events of the LoadVars Object.
[quote]Methods:
Method
LoadVars.load - Downloads variables from a specified URL.
LoadVars.getBytesTotal - Returns the number of bytes loaded from a load or sendAndLoad method.
LoadVars.getBytesTotal - Returns the total number of bytes that will be downloaded by a load or sendAndLoad method.
LoadVars.send - Posts variables from a LoadVars object to a URL.
LoadVars.sendAndLoad - Posts variables from a LoadVars object to a URL and downloads the server\'s response to a target object.
LoadVars.toString - Returns a URL encoded string that contains all the enumerable variables in the LoadVars object.[/quote]
[quote]Properties:
Property
LoadVars.contentType - Indicates the MIME type of the data.
LoadVars.load - A Boolean value that indicates whether a load or sendAndLoad operation has completed.[/quote]
[quote]Event:
LoadVars.onload - Invoked when a load or sendAndLoad operation has completed.[/quote]
There is a hidden method available in LoadVars class which was not documented in flash documentation.
This method can be used to internally decode name/value pairs into object properties and values. It accepts one argument as a string that contains name/value pairs and the string will be converted into object properties and associated values.
[code]Name/value pairs look like the following:
myvariable=value&myothervariable=anothervalue[/code]
This hidden method will convert the above name/value pairs into two properties "myvariable" and "myothervariable." The values of these properties are the associated values in the name/value pair string, so the value of "myvariable" in the above string is "value" and the value of "myothervariable" in the above string is "anothervalue." Here\'s a little code example to show you what this method does:
[code] // create a new instance of the LoadVars object
vat lv:LoadVars = new LoadVars();
// call the method with name/value pair string
lv.decode("name=Jhon&age=22");
// iterate over all the properties in the ‘lv’ object
for(var i in lv){
//trace the property name and property value
trace(i+":"+ lv [i])
}[/code]
The output of the above code will be:
[code]name:Jhon
age:22[/code]
Conclusion
The LoadVars Object is the powerful way to obtain simple communication with any server or fo loading data from text files. However, for complex data communication it is recommended to use WebService Connector component available in Flash MX 2004 Professional, or you can also use Flash Remoting MX for an effective and efficient method of communicatio. Flash Remoting MX uses Action Message Format (AMF), a binary message format designed for the ActionScript object model, which was modeled on the Simple Object Access Protocol (SOAP).AMF uses a packed format to relay information so the communication with the server will be efficient.
[url="http://www.devarticles.com/c/a/Flash/The-Power-of-LoadVars-Object/"]source[/url]
The Power of LoadVars Object
Moderators: Moderator, Global Moderator
Jump to
- General Category
- ↳ KillaNet Country
- ↳ 2D Graphics
- ↳ KillaNet News
- ↳ 3D & Animation
- ↳ Chatroom
- ↳ Flash
- ↳ Help & Suggestions
- ↳ Game Dev
- ↳ Audio & Video
- ↳ Introductions
- ↳ Journalism
- ↳ Phoenix Lounge
- ↳ Application Dev
- ↳ Toga Toga Toga!!!
- ↳ Photography
- ↳ Main Street Archives
- ↳ Web Design
- ↳ Fonts Icons Cursors & Screensavers
- ↳ Book Reviews
- ↳ General
- ↳ Tech Industry News
- ↳ Legal Resources
- ↳ Industry Contests
- ↳ Graphix Battle Arena
- ↳ KillaNet Contests
- ↳ Competition Archives
- ↳ Education Information
- ↳ Careers
- ↳ Game Studies
- ↳ Motivation
- ↳ Conferences & Seminars
- ↳ KillaDesign
- ↳ Animation & Film
- ↳ The Studio
- ↳ Game Development
- ↳ 2D Graphics
- ↳ Audio & Video
- ↳ 3D Graphics
- ↳ Flash
- ↳ Fonts, Icons & Emoticons
- ↳ Design Requests
- ↳ PhotoShop
- ↳ Cinema 4D
- ↳ Bryce
- ↳ Flash
- ↳ Paint Shop Pro
- ↳ Blender
- ↳ Poser
- ↳ The Darkroom
- ↳ Photo & Camera Discussion
- ↳ Photo Journalism
- ↳ Web Design Principles
- ↳ PHP & MySQL
- ↳ Designing For Print
- ↳ Writer\'s Desk
- ↳ Fiction Writing
- ↳ News Journalism
- ↳ Poetry
- ↳ Technical Writing
- ↳ Biographical Writing
- ↳ General Writing Resources
- ↳ Promotional Writing
- ↳ Film & Television
- ↳ VideoGames
- ↳ Computer Department
- ↳ General Discussion
- ↳ Windows Help
- ↳ Linux Help
- ↳ Builds & Mods
- ↳ Geek Gadgets
- ↳ Security
- ↳ Dev Discussion
- ↳ C++
- ↳ Visual Basic
- ↳ Java
- ↳ Application Skinning
- ↳ IRC Scripting
- ↳ Gaming Centre
- ↳ Guild Wars
- ↳ Aion
- ↳ Computer
- ↳ World of Warcraft
- ↳ Nintendo
- ↳ General RPG & MMORPG
- ↳ PlayStation
- ↳ XBox
- ↳ Other Consoles
- ↳ All Action
- ↳ Racers
- ↳ Sports
- ↳ FPS
- ↳ RTS
- ↳ Sim
- ↳ Casual Games
- ↳ Kids' Games
- ↳ Mobile Games
- ↳ Retro Games
- ↳ Challenges, Friendly Taunts & Discussion
- ↳ Game Requests & Bug Reports
- ↳ Open Source Games
- ↳ Puzzle Games
- ↳ HeadQuarters
- ↳ Uber Coffee Room
- ↳ KillaNet
- ↳ Coffee Room
- ↳ KillaNet
- ↳ KillaGraphix
- ↳ KillaHosting
- ↳ Marketing etc.
- ↳ Staff Issues
- ↳ Reference & Software
- ↳ Archives
- ↳ KillaBlogs
- ↳ Journalism
- ↳ Trash Can

