Showcase and discover digital art at yex

Follow Design Stacks

Subscribe to our free newsletter to get all our latest tutorials and articles delivered directly to your inbox!

Using the LoadVars Class to Load Variables

Using the LoadVars Class to Load Variables

The LoadVars class has three different methods: LoadVars.load, LoadVars.send and LoadVars.sendAndLoad. Basically, the load method loads variables from an external file or script, while the send method sends Flash variables to a remote script. The final method, sendAndLoad, is a combination of the first two methods. It sends variables from Flash to a server-side script and then waits for a response. This can be very useful if you need to send a user’s information to a script and then display a message depending on if the information was valid or not. For example, you may have built a simple login form where the user types in their username and password to gain access t o your site. If the username and password matched values in the database, you could do a gotoAndPlay(“welcome”) to let them enter the site. If the username and password didn’t match, you may want to display a message prompting them to try again, or else click on the register link to sign up for an account. Likewise, if you were building a game, you may want to send the user’s score to a server-side script wherich would insert the score into a table. If the user ranked within the top 20 scores you could forward them to a congratulations page.

You can see a brief example of the load method below, along with an event handler for the onLoad event, which gets triggered after the data has finished loading.

  1. var myLoadVars_lv:LoadVars = new LoadVars();
  2. myLoadVars_lv.load(“params.txt”, myLoadVars_lv);
  3. myLoadVars_lv.onLoad = function(success:Boolean) {
  4.     if (success) {
  5.         trace(“myLoadVars_lv loaded:”);
  6.         for (i in myLoadVars_lv) {
  7.             trace(” myLoadVars_lv.”+i+” = “+myLoadVars_lv[i]);
  8.         }
  9.     }
  10. };