The LoadVars Object

[ LiB ]

I have taken the information that I explained in the last section and saved it into a text file named GDA_TXT13.1.txt; it's under the Chapter 13 folder on the CD.

You will be using the LoadVars object to read in this information and manipulate it. Check out Figure 13.6 and see how this demo loads in the variables from this external file.

Figure 13.6. Loading variables from an external file

graphic/13fig06.gif


 Demo GDA_PROG13.2.fla does just thattake a look at the listing. // Game Development with ActionScript // By Lewis Moronta (c) 2003 // This program demos how to use // the LoadVars object to load // info from a small database // Create the object webData = new LoadVars(); // Load the properties of the file // NOTE: It has to be in the same // domain unless you are running // this program from your computer. webData.load("GDA_TXT13.1.txt"); // Setup the LoadVars callback function. // It is executed when the data is // finished loading. webData.onLoad = function (success) {   // Do something with the info...   if (success) {     // Verify the info       nameDisplay = webData.name;       scoreDisplay = webData.score;   } else {     trace("Error!");   } }; 

Assuming that the CGI program already created the database with that file name, this program can be easily modified to achieve an endless number of results. The first thing you do in order to load variables from an external file is create a LoadVars object, like this:

 // Create the object webData = new LoadVars(); 

The next thing to do is load the information. The information must be encrypted, just as I explained before. An HTML form does this encryption automatically when the information is sent to the CGI script. However, when the information is loaded into Flash, it is parsed into understandable data.

 // Load the properties of the file // NOTE: It has to be in the same // domain unless you are running  // this program from your computer. webData.load("GDA_TXT13.1.txt"); 

As information is not immediately loaded (because it takes so long to download), you must check to see when all the information is done loading. If you do not do this, you will end up with undefined variables. To avoid ending up with a bunch of undefined variables, you can use a callback function that is called just as soon as all the variables are loaded.

 webData.onLoad = function (success) { 

As you can see, there is one parameter that is sent to the function. It is a Boolean success value. If everything went well (success == true) , a true value means everything was loaded; if the loading didn't go well then the success value will be false and the program won't parse the data.

In this program, I set the code to display the information in two textboxes on the screen. The information variables become properties within this object, as you can see here:

 // Do something with the info... if (success) {   // Verify the info     nameDisplay = webData.name;     scoreDisplay = webData.score; } else {   trace("Error!"); } 

Just in case you were wondering, nameDisplay and scoreDisplay are the variables attached to the two textboxes on the stage.

Notice one thingthe data is still in string format and the information is still divided by commas. You can't use it like that, so what can you do? You have to use the split command to divide the information into arrays. This will allow you to use 1 index to access both of the pieces of information, including the name and the score of each individual. Let's see how in the following listing.

 // Game Development with ActionScript // By Lewis Moronta (c) 2003 // This program demos how to use // the LoadVars object to load // info from a small database // Create the object webData = new LoadVars(); // Load the properties of the file // NOTE: It has to be in the same // domain unless you are running   // this program from your computer. webData.load("GDA_TXT13.1.txt"); // Setup the LoadVars callback function. // It is executed when the data is // finished loading. webData.onLoad = function (success) {   // Do something with the info...   if (success) {     displayData();   } else {     trace("Error!");   } }; function displayData() {   // Divide the information up   namesArray = webData.name.split(",");   scoresArray = webData.score.split(",");   // Store the information in a viewable format   name1Display = namesArray[0];   score1Display = scoresArray[0];   name2Display = namesArray[1];   score2Display = scoresArray[1];   name3Display = namesArray[2];   score3Display = scoresArray[2]; } 

If you look at Figure 13.7, you'll see how I neatly organized the information into six different boxes.

Figure 13.7. Manipulating individual pieces of data

graphic/13fig07.gif


In this program, I created the object then loaded and parsed the information, like this:

 // Create the object webData = new LoadVars(); // Load and parse... webData.load("GDA_TXT13.1.txt"); 

After that, I decided to set up the object's onLoad function so that it can call another function if everything is successful. The other function is displayData .

 webData.onLoad = function (success) {   // Do something with the info...   if (success) {     displayData();   } else {     trace("Error!"); } 

I declared displayData with no parametersit's a generic function name too.

 function displayData() { 

The first thing I did in this function was to divide the information up into elements of an array. I did this using the split method of a string.

 // Divide the information up namesArray = webData.name.split(","); scoresArray = webData.score.split(","); 

Now that all the info is set into elements of an array, all I have to do is access each pair with one indexhow cool is that?

NOTE

TIP

As you're now dealing with arrays, you can use all the sorting and manipulation functions and methods that you learned before. This is great when you want to output lists and other things, like a high score screen perhaps.

In order to display the information, all I did was access the current name and score for each index.

 // Store the information in a viewable format name1Display = namesArray[0]; score1Display = scoresArray[0]; 

The rest of the information was populated in much the same way.

 name2Display = namesArray[1]; score2Display = scoresArray[1]; name3Display = namesArray[2]; score3Display = scoresArray[2]; 

And that's how to retrieve and organize information from the server.

[ LiB ]


Game Development with ActionScript
Game Development with ActionScript
ISBN: 1592001106
EAN: 2147483647
Year: 2004
Pages: 162
Authors: Lewis Moronta

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net