Loading Text Data


In general, data is a collection of facts. When you see the word data in a programming book, it usually refers to a formatted set of numbers and letters.

With Flash, you have many options for visualizing data to help the end user gain meaning from that data. You can use a dynamic text field to display data that is as simple as a single string, such as "The cat is brown." You can also visualize a large and complex set of data points with an interactive graph that's been dynamically generated.

The LoadVars() Object

Previous chapters talked about using event handlers to execute specific code when an event occurs. The LoadVars object has a useful event handler called onLoad(), which is invoked when data completes loading into the LoadVars object.

You might use LoadVars when you are passing simple data, such as a short list of guests, to the .swf. This is very useful when you want to update information displayed in the published .swf, without changing the source .fla and republishing. You could keep a list of guests in an external file and edit it at a later time.

You can use the LoadVars() object to load data from an external text file in a variable/value pair format. The data in the external text file must be formatted as follows:

&guest1="Jill Smith"


The & symbol signals to the Flash Player that a variable/value pair follows that needs to be passed into a LoadVars object of the .swf file. There must be no spaces between the variable name, the = operator, and the first character in the value.

To use the LoadVars object, first create a new instance of the LoadVars object and give it a name. Next, assign a function to execute when the data has completed loading. Finally, load your external data file.

In the following example, a text file is loaded into a new LoadVars object (guestList). The onLoad function traces the value of the variable name1 to the Output window. Because the function is attached to the guestList instance, the this keyword refers to the guestList instance of LoadVars, which is holding the newly loaded data. Variable names in the original data become property names of the guestList instance and can be accessed as you would access a property of any instance.

// create new LoadVars object guestList = new LoadVars(); // function to execute when data completes loading guestList.onLoad = function(){     //trace guest name     trace(this.guest1); } // load data into my LoadVars guestList.load("guest_list_data.txt");


You can also pass data from your movie with the LoadVars object. Instead of a .txt file, you might want to load a .cfm file instead and pass variable/value pairs to a query. In this case, format your URL address much as you would with the getURL example.

myLoadVars.load("loadVarsData.cfm?a=22&b=12");


You can also send data with the send() method of the LoadVars object:

myLoadVars = new LoadVars(); myLoadVars.a = 22; myLoadVars.b = 12; myLoadVars.send("loadVarsData.cfm", "_self", "GET");


As you can see, the LoadVars object is very useful for loading and handling simple external data. The next section introduces you to how this external data should be formatted.

Embedding Data in a Text Document

The LoadVars example used a .txt source file with the data formatted as follows:

&a=22&b=12&c="this is a string, and can have whitespace"


Whitespace can interfere with the data parsing process in this format, so be careful about leaving spaces between elements in your data unless they are between quotes, in which case you can leave spaces that will be read and formatted when they go into a text field.

You can also embed data in the Embed and Object tags used to embed the .swf file into an HTML document. This can be used to pass data needed to initialize your application. The following is a simple example of this technique. Note that the variable is created just after the source name.

<OBJECT> <PARAM NAME="movie" VALUE= "movie.swf?title=Gallery&imageFiles=images/image1.jpg"> <EMBED src="/books/1/325/1/html/2//support/flash/ts/documents/movie.swf?title=Gallery&imageFiles=images/image1.jpg"> </EMBED> </OBJECT>


What this code does is tell the .swf file embedded in this HTML document that the value of title is Gallery, and that the value of imageFiles is images/image1.jpg. These variable/value pairs can now be used by the .swf.



Special Edition Using Macromedia Studio 8
Special Edition Using Macromedia Studio 8
ISBN: 0789733854
EAN: 2147483647
Year: 2003
Pages: 337

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