Loading variables from a server

There are several ways to load variables into Flash from external sources (such as text files, XML documents, and so on). You can find much more information on loading variables, including name/value pairs, in Working with External Data.

In Flash, you can easily load variables using the LoadVars class, as shown in the next example.

To load variables from a server:

  1. Create a new Flash document.
  2. Select Frame 1 of the Timeline, and add the following ActionScript in the Actions panel:
    var my_lv:LoadVars = new LoadVars();
    my_lv.onLoad = function(success:Boolean):Void {
        if (success) {
            trace(this.dayNames); // Sunday,Monday,Tuesday,...
        } else {
            trace("Error");
        }
    }
    my_lv.load("http://www.helpexamples.com/flash/params.txt");
    

    This code loads a text file from a remote server and parses its name/value pairs.

    TIP

     

    Download or view the text file (http://www.helpexamples.com/flash/params.txt) in a browser if you want to know how the variables are formatted.

  3. Select Control > Test Movie to test the document.

    If the file successfully loads, the complete event is called and the Output panel displays the value of dayNames. If the text file cannot be downloaded, the success argument is set to false and the Output panel displays the text Error.


Flash CS3


 

Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/9.0/main/00000669.html