Understanding LoadVars


You will be using the LoadVars class throughout the Tech Bookstore. Now that you have a better understanding about classes, objects, methods, properties, functions and conditional statements, you will look at the LoadVars class as an example of how to start putting these concepts together in a workable form.

LoadVars is a simple way to build dynamic websites that are easy to update. LoadVars can load variables defined in an external text file into Flash on the fly. Those variables then become properties of the LoadVars object that you're using to load data and can be references as such throughout the rest of your ActionScript. Because the LoadVars object is loading data from a text field when the SWF file runs in the browser, any changes to the data can be made in the text file itself to update the Flash file. No need to republish! Another benefit of using LoadVars is when you're using any server-side languagesuch as ColdFusion, PHP, ASP or Javait is possible to have your server-side language query a database and write the latest news articles to a text file that can then be loaded by an SWF file.

There are three different ways of using Flash and LoadVars: send, sendAndLoad, and load. send simply sends data to a server where it can be processed by server-side scripts and entered into a database, appended to an XML document, sent as an e-mail, or used however you design the server-side solution to work. Using sendAndLoad sends the data to the server, but also accepts a response from the server and places the result in a LoadVars object, in which the variables can be manipulated or displayed using Flash. Loading and using variables can be useful for the Tech Bookstore if you want to send an ISBN number to a template on your server and have the server-side software query a database, grab a book review or information based on that book, and return the result to Flash to be displayed in the SWF file. The final method, load, is what you'll mainly be using throughout the Tech Bookstore site. The load method loads variables from a text file, and you'll display the variables in TextArea component instances or elsewhere throughout the Tech Bookstore application.

A sample text file, which can be used by Flash, could look similar to the following listing:

&name=Sue&

By loading in the previous simple text file using the sendAndLoad or load method, Flash creates a new variable in the target LoadVars object named name and gives it a value of Sue. You can add as many other variables to the text file by separating each name/value pair with an ampersand (&) and an equals sign (=), as in the following example:

&name=James& &position=mentor& &manager=Nate W&

The code here creates three separate variables in Flash: name, position, and manager. The value of name is set to James, the value of position is set to mentor, and the value of manager is set to Nate W. To load these values into Flash using the LoadVars class, save the listing into a text file, in this case named testfile.txt, and add the following ActionScript code to a blank Flash document:

var test_lv:LoadVars = new LoadVars(); test_lv.load("testfile.txt"); test_lv.onLoad = function(success:Boolean) {     trace(this.name); };

Because you're using LoadVars for this example, you have to save the Flash document to the same folder as the testfile.txt document before you test the sample code.

Tip

You can place the text files in a directory different from the SWF file that will load them. You just have to make sure that you reference the file correctly in the load method: test_lv.load("textFiles/testFile.txt"), and so on.


The code creates a LoadVars object named test_lv. You load in the external file using the load method in the LoadVars class, which happens in the second line of ActionScript. The method takes a single parameter, which is the path to the file that you want to load. In this case, you're loading in a file named testfile.txt, which is in the same folder as the current Flash document.

The next piece of code might be a little tricky to understand. The LoadVars class also has a couple of events that Flash triggers when certain things occur. In the code, the event that is being triggered is onLoad, which Flash triggers when the text file defined in the load method has been completely loaded into Flash. The code simply says when the specified text file has finished loading, execute the following code. After the file has been completely loaded and the onLoad event triggers, your three variables defined in the testfile.txt file are stored in the test_lv LoadVars object. You can now trace the value of test_lv.manager and see the value Nate W in the Output panel. In the previous code listing, you are using what is known as an inline, or anonymous, function, which is simply a function that hasn't been given a name.

An inline function does the same thing as named functions. There are some differences, and one is that they're usually attached to a specific object and perform a specific task. Another is that they don't hang around in memory waiting to be called. The function is created when an event happens, executes in response, and then is removed from memory until the next time it is needed.




Macromedia Flash 8. Training from the Source
Macromedia Flash 8: Training from the Source
ISBN: 0321336291
EAN: 2147483647
Year: 2004
Pages: 230
Authors: James English

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