| |
|
|
|
|
|
|
|
Loading variables from a text file using the LoadVars class
download source files
From the Actionscript 2.0 language reference
You can use the LoadVars class to obtain verification of successful data loading and to monitor download progress. The LoadVars class is an alternative to the loadVariables() function for transferring variables between a Flash application and a server.
The LoadVars class lets you send all the variables in an object to a specified URL and to load all the variables at a specified URL into an object. It also lets you send specific variables, rather than all variables, which can make your application more efficient. You can use the LoadVars.onLoad handler to ensure that your application runs when data is loaded, and not before.
The LoadVars class works much like the XML class; it uses the load(), send(), and sendAndLoad() methods 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 Document Object Model (DOM) tree stored in the XML object. The LoadVars class follows the same security restrictions as the XML class.
Availability: ActionScript 1.0; Flash Player 6
Tutorial
The LoadVars class has been introduced to replace the old deprecated loadVariables and loadVariablesNum global function. The LoadVars methods give us a lot of control on the process of loading data from a file or a server. In our case here we are going to use it to load variables from a text file. Let's start by a simple tutorial :
The first thing we want to do is to create a text file that will contain a couple of variables that we want to load in our Flash application. You need to set your variables in the text file in url-encoded format because this is how Flash will be able to read your variables using the LoadVars method, more info about that format can be found at Adobe.com
1. open Notepad and enter the below url-encoded set of variables:
myName=robert&myAge=86&myPlanet=Mars
2. save your text file as in the same directory where your Flash file resides.
3. Create a new Flash file and on frame 1 create a new instance of the the LoadVars class using the new keyword. For people familiar with Object Oriented Programming nothing new here. If you are not familiar with OOP it's not a problem just remember that this is how you need to declare your LoadVars instance.
| myVariables = new LoadVars(); |
4. We have created an instance of the Loadvars class. To load the variables into the newly created instance we we are going to use the method load(url:String). We need to specify the URL that point to the file that contains our variables. Notice that the URL need to be typed between quotes.
myVariables = new LoadVars();
myVariables.load("myFile.txt"); |
5. Finally we are going to assign a function to the onLoad event handler that will be called when the variables have finished loading.
myVariables = new LoadVars();
myVariables.load("myFile.txt");
myVariables.onLoad = function(success){
if(success){
trace("My name is "+myVariables.myName+", I am "+myVariables.myAge+" and I live on "+myVariables.myPlanet+".");
} else {
trace("there was a problem loading the variables");
}
}
|
Before we do anything with the variables we need to be sure they have loaded in the myVariables instance of the LoadVars class, that is why we have associated a function to the onLoad event that will be called as soon as the variables have finished loading. The following code is wrong and won't work because when the trace method is called the variables haven't loaded yet.
myVariables = new LoadVars();
myVariables.load("myFile.txt");
trace("My name is "+myVariables.myName+", I am "+myVariables.myAge+" and I live on "+myVariables.myPlanet+".");
|
|
|
|
If you think this page is providing useful information, don't hesitate to leave a comment.
|
|
|
|
|
|
|
|
|
Copyright ©2006-2008 flashvalley.com - All rights reserved |
|