Macromedia Flash 8 Bible Free Open Book

Macromedia Flash 8 Bible

Previous Page
Next Page

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="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.


Previous Page
Next Page
Index: [A][B][C][D][E][F][G][H][I][J][K][L][M][N][O][P][Q][R][S][T][U][V][W][X][Y][Z]


     Main Menu
Table of Contents
Back Cover
Macromedia Flash 8 Bible
Foreword
Preface
Part I: An Introduction to Flash Web Production
Part II: Mastering the Flash Environment
Part III: Creating Animation and Effects
Part IV: Integrating Media Files with Flash
Part V: Adding Basic Interactivity to Flash Movies
Part VI: Distributing Flash Movies
Part VII: Approaching ActionScript
Chapter 24: Knowing the Nuts and Bolts of Code
Breaking Down the Interactive Process
The Basic Context for Programming in Flash
One Part of the Sum: ActionScript Variables
Declaring Variables in ActionScript
Creating Expressions in ActionScript
Creating a Login Sequence with Variables
Summary
Chapter 25: Controlling Movie Clips
Chapter 26: Using Functions and Arrays
Chapter 27: Interacting with Movie Clips
Part VIII: Applying ActionScript
Part IX: Integrating Components and Data-Binding
Part X: Expanding Flash
Part XI: Appendixes
Index
List of Figures
List of Tables
List of Listings
List of Sidebars


More Books
PHP Hacks
Processing Xml With Java - A Guide To Sax, Dom, Jdom, Jaxp, And Trax
The Koran (Holy Qur'an)
Macromedia Flash 8 Bible
Search Engine Optimization for Dummies
YouTube Traffic
PHP 5 for Dummies
Harry Potter and The Chamber of Secrets
Harry Potter and the Sorcerer's Stone
The Pilgrim's Progress
Wireless Hacks
Flash Hacks. 100 Industrial-Strength Tips & Tools
PayPal Hacks. 100 Industrial-Strength Tips and Tools
Amazon Hacks
Pdf Hacks
The Da Vinci Code
Google Hacks
The Holy Bible
Windows XP For Dummies
Harry Potter and the Half-Blood Prince
Seo Book
Upgrading and Repairing Networks
Macromedia Dreamweaver 8 UNLEASHED
Windows XP Annoyances
Windows XP Hacks
Microsoft Windows XP Power Toolkit
Teach Yourself MS Office In 24Hours
iPod & iTunes Missing Manual
PC Hacks 100 Industrial-Strength Tips and Tools
PC Overclocking, Optimization, and Tuning - 2th Edition
PC Hardware In A Nutshell 3rd Edition
PC Hardware in a Nutshell, 2nd Edition
Upgrading and Repairing PCs
Google for Dummies
MySQL Cookbook
Teach Yourself Macromedia Flash 8 In 24 Hours
PHP CookBook
Sams Teach Yourself JavaScript in 24 Hours
PHP5 Manual
Free Games Paper Airplanes
500 Juegos Gratis 500 Giochi Gratis 500 Jeux Gratuits 500 Jogos Gratis 500 Kostenlose Spiele