User Guide
278 Chapter 11: Working with External Data
For example, if you want to track the high scores for a game, you could store the scores on a server
and use
loadVariables() to load them into the SWF file each time someone played the game.
The function call might look like the following example:
loadVariables("http://www.mySite.com/scripts/high_score.cfm", _root.scoreClip,
GET);
This example loads variables from the ColdFusion script called high_score.cfm into the movie
clip instance
scoreClip using the GET HTTP method.
Any variables loaded with the
loadVariables() function must be in the standard MIME format
application/x-www-form-urlencoded (a standard format used by CFM and CGI scripts). The file
you specify in the
URL parameter of loadVariables() must write out the variable and value pairs
in this format so that Flash can read them.This file can specify any number of variables; variable
and value pairs must be separated with an ampersand (
&), and words within a value must be
separated with a plus (
+). For example, the following phrase defines several variables:
highScore1=54000&playerName1=RGoulet&highScore2=53455&playerName2=
WNewton&highScore3=42885&playerName3=TJones
Note: You might need to URL-encode certain characters, such as the plus (+) or ampersand (&)
characters. For more information, see www.macromedia.com/support/flash/ts/documents/
url_encoding.htm.
For more information, see “Using the LoadVars class” on page 278. Also, see loadVariables(),
getURL(), loadMovie(), and the LoadVars class entry in ActionScript Language Reference
Help.
Using the LoadVars class
If you are publishing to Flash Player 6 or later and want more flexibility than
loadVariables()
offers, you can use the LoadVars class instead to transfer variables between a SWF file and a server.
The LoadVars class was introduced in Flash Player 6 to provide a cleaner, more object-oriented
interface for the common task of exchanging CGI data with a web server. 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 (although you can’t access
the data until it is fully downloaded).