• home
  • forum
  • my
  • kt
  • download
  • LoadVars Class in Flash

    Author: 2007-06-14 10:15:42 From:

    LoadVars class is the most powerfull class for communicating with server-side technologies and to load external data from text files. It 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 and text files.

    LoadVars class is an alternative to the loadVariables action with more flexibility for transferring variables between a Flash movie and a server-side technologies like ASP, ASP.NET, PHP, JSP, ColdFusion, CGI-Perl, etc.

    You can use the LoadVars class to obtain verification of successful data loading, progress indications, and stream data while it downloads. The LoadVars class works much like the XML class; it uses the methods load, send, and sendAndLoad to communicate with a server. The main difference between the LoadVars class and the XML class is that LoadVars transfers ActionScript name and value pairs, rather than an XML DOM tree stored in the XML class. It also lets you to send variables only that are required by the server instead of sending all the variables as in the case of loadVariables or loadVariablesNum.

    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 to track errors in communication.
    • You can add custom HTTP request headers.
    • You must create a LoadVars object to call its methods. This object is a container to hold the loaded data.

    Using LoadVars Object for communicating with the server:

    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 server
    3)Use sendAndLoad method to send the associated variables to server as POST/GET variables and also specify the LoadVars object for receiving variables from 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 if 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 loadedDotNetVars 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 if you are sending arrays to the server those array values are sent as a comma-delimited list


    Ex: If you are sending the following data
    send_lv.type = "BUTTON";
    send_lv.id = "1";
    send_lv.colors = ["red", "white", "blue"];


    the query string will look like:


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


    Receiving the variables in server-side script:

    You can access the flash variables in the server-side script normally like you access form POST/GET variables. The variable names will be the names you use when associating the variables to the LoadVars Object.


    Example 1 (ASP): Request(¡°type¡±);
    Example 2 (PHP): $_POST['type']
    Example 3 (ColdFusion): #FORM. type#


    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.


    &name=John&id=342&ecode=e123


    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
    Response.Write("&name=John&id=342&ecode=e123")

    Eample 2 (PHP):
    You can use print or echo language constructs to write data in PHP.
    echo "&name=John&id=342&ecode=e123";

    Eample 3 (ColdFusion):
    You can use tag to output data to flash.
    &name=John&id=342&ecode=e123


    Other methods of LoadVars Object for communicating with the server:

    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.


    LoadVars.load("asp_net_php_page");


    If you want to only send data to the server you can use LoadVars.send method.


    LoadVars.load("asp_net_php_page", "_blank", "POST");


    Using LoadVars Object for loading data from text files:

    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


    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


    Hidden Method of LoadVars class:

    There is a hidden method available in LoadVars class which was not documented in flash documentation.


    LoadVars.decode


    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.
    Name/value pairs look like the following:


    myvariable=value&myothervariable=anothervalue


    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'. Heres a little code example to show you what this method does:


    // 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])
    }

    The output of the above code will be:
    name:Jhon
    age:22


    Conclusion:

    The LoadVars Object is the powerful way for simple communication with any server or for 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 most effective and efficient way of communication. Flash Remoting MX uses Action Message Format (AMF), a binary message format designed for the ActionScript object model which was modelled on the Simple Object Access Protocol (SOAP). AMF uses a packet format to relay information so the communication with the server will be efficient.

    discuss this topic to forum

    relation tutorial

    No relevant information

    Category

      3D (20)
      Math Physics (14)
      3rd Party (5)
      Navigation (60)
      Actionscripting (26)
      Optimization (16)
      Animation (32)
      Projector (9)
      Audio (46)
      Special Effects (112)
      Backend (25)
      Text Effects (65)
      Drawing (18)
      Tips and Techniques (41)
      Dynamic Content (25)
      Tricks (6)
      Games (66)
      Utilities (19)
      Getting Started (71)
      Video (10)
      Interactivity (21)
      Web Design (22)

    New

    Hot