Using the LoadVars class

You can use the LoadVars class instead of loadVariables() to transfer variables between a SWF file and a server. The LoadVars class lets you send all the variables in an object to a specified URL and load all the variables at a specified URL into an object. The response from the server triggers the LoadVars.onLoad() method and sets variables in the target. You can use LoadVars to obtain error information and progress indications and to stream the data while it downloads.

The LoadVars class is similar to the XML class; it uses the methods load(), send(), and sendAndLoad() to initiate communication with the 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 DOM (Document Object Model) tree stored in the XML object.

You must create a LoadVars object to call its methods. This object is a container to hold the loaded data.

The following procedure shows how to use a LoadVars object to load variables from a text file and display those variables in a text field.

To load data with the LoadVars object:

  1. In a text editor such as Notepad or SimpleText, create a text file and add the following text to the text file:
    day=11&month=July&year=2003
    
  2. Save the file as date.txt.
  3. In Flash, create a document.
  4. Create a dynamic text field on the Stage and give it the instance name date_txt.
  5. Select Frame 1 in the Timeline and open the Actions panel (Window > Development Panels > Actions) if it isn't already open.
  6. Enter the following code in the Actions panel:
    var dateVars = new LoadVars();
    dateVars.onLoad = function(ok) {
      if (ok) {
        date_txt.text = dateVars.day+"/"+dateVars.month+"/"+dateVars.year;
      }
    };
    dateVars.load("date.txt");
    

    This code loads the variables in data.txt (day, month, year), then formats and displays them in the text field date_txt.

  7. Save the document as dateReader.fla to the same directory that contains date.txt (the text file you saved in step 3).
  8. Select Control > Test Movie to test the document.

For more information, see LoadVars class.