Declaring Variables in ActionScript


There are several ways to establish, or declare, variables in a Flash movie. You can create them directly with ActionScript (or with TextField objects, as shown in the last section), load them from a text file or server-side script, or include them in HTML tags.

Using Actions to Define Variables

The most common way to create a variable is to type the variable's name and value in the Script pane of the Actions panel, on a specific timeline's keyframe. Most basic variables will have values that are string literals.

Loading Variables from a Predefined Source

You can also establish variables by loading them from an external source, such as a text file located on your Web server or even through a database query or a server-side script. By using the loadVariables() action, you can load variables in this fashion. There are three primary parameters for the loadVariables() action: URL, target, and variables:

 loadVariables(url, target, [,variables]); 

The variables parameter is optional. In the Help panel pages in the ActionScript Dictionary, any parameters displayed with surrounding left and right brackets ([ ]) are optional.

Note 

There's more than one way to load data into Flash Player 5 and higher compatible movies. While you're learning the basics of ActionScript, we recommend that you use the loadVariables() function to familiarize yourself with data-loading concepts. If you're publishing Flash Player 6 or higher movies, you should learn the syntax of the LoadVars class.

URL specifies the source of the variables to be loaded. This can be a relative link to the variable source (you don't need to enter the full path of the resource). You can specify whether this URL value is a literal value ("http://www.theMakers.com/cgi-bin/search.cfm") or an expression that uses a variable or a combination of variables (serverURL + scriptPath + scriptApp). If you want to point to a specific file, type its relative path and name here. If you want to access a database that returns dynamic data, insert the path to the script, such as "http://www.domain.com/cgi-bin/search.pl".

The target parameter determines where the variables are to be loaded. You can send the name/value pairs to a level or a timeline target. If you want the variables to be available on the Main Timeline, use _root or _level0. You can also specify a Movie Clip target using a relative or absolute address. To load to the current Movie Clip (the one initiating the loadVariables action), use the target this.

The last option is variables, and this parameter specifies whether you are sending and loading (in other words, receiving) variables. If you want to load variables from a static source, such as a text file, you should omit this parameter. If you are sending a query to a database driven engine, then you may need to specify either "GET" or "POST". Note that the use of loadVariables() in the GET or POST method means that you are sending variables declared on the active timeline to the specified URL, which, in turn, will send name/value pairs back to the Flash movie.

The formatting of name/value pairs is standard URL-encoded text. If you want to encode name/values in a text file (or a database), you need to use the following format:

 variable=value&variable=value... 

Basically, in URL-encoded text, name/value pairs are joined by an ampersand (&). To join multiple terms in a value, use the plus (+) symbol to add a single whitespace character between the terms, as in:

 name1=Joe+Smith&name2=Susan+Deboury 

Cross-Reference 

There are several ways to load data into a Flash movie. Flash Player 5 introduced the XML object, and Flash Player 6 introduced the LoadVars object, both of which give you more control over dynamic data. The LoadVars object is discussed in Chapter 29, "Sending Data In and Out of Flash." You also learn more about URL-encoded text in that chapter.

Sending Variables to URLs

You can also send variables to a URL by using the getURL() action. Any name/value pairs that are declared on the active timeline will be sent along with the getURL() action if a variable send method is defined (GET or POST). Note that getURL() is only used to send variables out of a Flash movie — it will not retrieve or load any subsequent name/value pairs. If you use a getURL() action on a Movie Clip timeline as follows:

 var firstName = "Robert"; getURL("/cgi-bin/form.cgi", "_blank", "GET"); 

the Flash movie will send the following request to your server:

  • http://www.server.com/cgi-bin/form.cgi?firstName=Robert;

The output of the form.cgi script would be opened in a new browser window ("_blank").

Note 

Any terms that follow a question mark (?) in a URL are called a query string. The name/value pairs in a query string are usually provided as an input or a filter to a server-side script.

Establishing Variables with HTML

You can also send variables to Flash movies in the <embed> and <object> tags that call the Flash movie. In the SRC attribute of <embed> or the param name=" movie" subtag of the<object> tag, attach the name/value pairs to the end of the Flash movie filename, separated by a question mark (?).

 <object ... > <param name="movie" value="flash.swf?name=Rob" /> <embed src="/books/4/403/1/html/2/flash.swf?name=Rob" ... /> 

This method works with Flash Player 4 or later movies. However, Flash Player 6 and higher can recognize the HTML attribute flashvars. Now, you can specify longer strings of name/value pairs that will be declared in the Flash movie as soon as it loads into the Flash Player. The previous method that we showed is limited to strings that are about 1,024 characters long (depending on the Web browser), which include the movie's full URL. With the newer flashvars attribute, you don't need to rely on the URL of the movie. Here's an example of declaring a few variables with the flashvars attribute:

 <object ...> <param name="movie" name="flash.swf" /> <param name="flashvars" name="firstName=Gregory&lastName=Smith&address=1234+Hollywood+Way" /> <embed src="/books/4/403/1/html/2/flash.swf" flashvars="firstName=Greg&lastName=Smith&street=1234+Broadway" ... /> 

In practical use, you would not likely hard code the name/value pairs directly into the HTML document. You can use client-side JavaScript or server-side scripting (such as PHP, ASP.NET, or ColdFusion) to dynamically "write" the name/value pairs into the HTML document before it is served to the user's Web browser.




Macromedia Flash 8 Bible
Macromedia Flash8 Bible
ISBN: 0471746762
EAN: 2147483647
Year: 2006
Pages: 395

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