Go Back   The Clock Crew
Register FAQ Members List Search Today's Posts Mark Forums Read

Tutorials:flash:actionscript:using_a_loadvars_object
Home   Backlinks   8 Views   Index   Random  
   

–By PineApple–

A LoadVars object in flash can be used to send and recieve information from remote servers. Usually, one would use loadVariables() for this purpose, but a few disadvantages arise from using loadVariables with POST, such as the fact that the entire scope of variables available to that call (all global variables) will be sent to the server if you use a GET or POST method, and that you have to specify a reciever object for that call and add a listener to its Data event.

To combat this, flash has a LoadVars class, which lets you send data to and from a server more comfortably than the loadVariables method.

Lets say we have a SWF that is going to take the contents of a text box in flash and send them to a PHP script. The PHP script will then scramble the letters and return it back to the SWF, and the output will be displayed in the text field.

To do this with LoadVars, we would first need to create an input text on the stage. Name this input text “txtInput” and lets get down to our actionscript. Create a button and name it “btnInput”.

in the actions panel for the button:

on(press) {
   _root.pressButton();
}

now, in the first frame of the movie, place this actionscript:

 
//a function for handling the user pressing the button
function pressButton() {
   var textToSend = txtInput.text;
   txtInput.text = "";
   sendToServer(textToSend);
}

Here we are taking the text from the input field we created earlier, and storing the text inside a temporary variable textToSend. We then use this as a parameter to the sendToServer() function we will write next:

function sendToServer(n) {
lv = new LoadVars(); //creates a LoadVars object

Great! now we have a LoadVars object, but its not going to do anything until we call one of its methods and have it perform some action. Before we perform any actions, we need to do one thing: Whenever you make a object in AS, determine which (if any) events you want to be notified of. In this case, our LoadVars object has a event called onLoad that we want to listen for. This event will happen once our PHP script sends something back to the LoadVars object.

This is how we listen for this event:

lv.onLoad = function(success) {
   if (success) {
      //we got a response, now lets feed that
      //response to a handler function
      handleResponse(lv);
   } else {
      trace("server response error");
   }
}

Now, we declared an anonymous function (a function with no name) that will execute whenever the onLoad event of our LoadVars object gets called. That function automatically gets passed the success parameter. You can see what parameters events pass in the actionscript dictionary in order to write anonymous functions for them.

Lets send some data to our PHP script:

lv.textparameter = n;
lv.sendAndLoad("http://www.yourserver.com/yourscript.php",lv,"POST");
} // end function

Now the first line in there assigns n (the parameter we passed the sendToServer function) to a property of the lv object called textparameter. So the LoadVars object we created now has a new property, called textparameter, and it contains what our text field contained.

What happens here is quite nice: the LoadVars object takes the parameter and PHP sees it as a $_POST variable. Lets quickly create our PHP script to handle the data:

“yourscript.php”

<?
$incomingtext = $_POST['textparameter']; //get the POST data
$shuffled = str_shuffle($incomingtext); //shuffle it
echo "&scrambledText=$shuffled&done=yes";
?>

the actual text output of the PHP script will look something like this:

&scrambledText=ksemaow&done=yes

Why do we put the done=yes on the end? I use this convention in case the data gets cut off for some reason, we can be sure by checking the value of the done variable whether the message was recieved in full.

Now for the final touch, we need to grab the incoming text and throw it back in the input field in flash. When the anonymous function gets called in flash, it sends the lv object, which will now magically have the scrambledText and done properties.

function handleResponse(lv) {
  _root.txtInput.text = lv.scrambledText;
}

Thats all! Now upload your PHP file and start your SWF up, and try it out. Good luck and happy scripting!

– PineappleClock

Here is the full actionscript for the FRAME 1 actions:

 
//a function for handling the user pressing the button
function pressButton() {
   var textToSend = txtInput.text;
   txtInput.text = "";
   sendToServer(textToSend);
}
 
function sendToServer(n) {
   lv = new LoadVars(); //creates a LoadVars object
   lv.onLoad = function(success) {
     if (success) {
        //we got a response, now lets feed that
        //response to a handler function
        handleResponse(lv);
      } else {
        trace("server response error");
      }
   }
   lv.textparameter = n;
   lv.sendAndLoad("http://www.yourserver.com/yourscript.php",lv,"POST");
} // end function
 
function handleResponse(lv) {
   _root.txtInput.text = lv.scrambledText;
}
 

 

Powered by vBulletin® Version 3.6.3
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.