|
Free Open Book
Macromedia Flash 8 Bible |
Declaring Variables in ActionScriptThere 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 VariablesThe 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 SourceYou 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.
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
Sending Variables to URLsYou 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:
The output of the form.cgi script would be opened in a new browser window ("_blank").
Establishing Variables with HTMLYou 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="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="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. |
Main Menu |
| 500 Juegos Gratis | 500 Giochi Gratis | 500 Jeux Gratuits | 500 Jogos Gratis | 500 Kostenlose Spiele |