A variable is a container that holds information, such as numerical or string data. This TechNote outlines the five main ways to set variables in a Flash movie. For more information on variables, refer to page 191 of the Flash 4 manual, "Setting and identifying variables".
There are five main ways to set variables in Flash:
Use the Set Variable action in Flash. This example uses a frame action, though it can also be done using a button action.
To set a variable using "Set Variable":
Choose Control > Test Movie to test this. The variable named "text" will be assigned the specified value "hello" when the movie plays the first frame of the timeline.
Also, in this example, setting the variable will not show any visual change in the movie. However, variables and their values can be previewed in Test Movie mode by choosing Control > List Variables.
Note: To the right of the "Variable" and "Value fields", there is a pop-up menu, which contains "String Literal", "Expression", and "Expression Editor". The first time a variable is set, make sure the variable field is set to "String Literal". For more information creating expressions, please refer to page 196 of the Flash 4 manual.
Text fields in Flash can be used to assign a value to a variable by user text entry, or to display the value of a variable. For more information on creating text fields, see page 117 of the manual for more information on text fields, and Using Type Blocks and Text Fields in Flash (TechNote 14154).
To set a variable using a text field:
Note: A value you assign to a variable will also display in a text field with the same variable value. (For instance, if "text" is set to "hello", the word "hello" will display in the text field with variable name "text".)
Variables can be obtained from a remote file by using the action Load Variables. The remote file can be a text file or a server-side application such as ASP, CGI or ColdFusion. This example uses a text file as the data source, with the command issued by a frame action.
To obtain external data using Load Variables:
Note: Special characters (such as punctuation, numeric operators, and other non-alphanumeric data) must be URL encoded to be translated into the Flash movie. For more information, see URL Encoding: Reading special characters from a text file (TechNote 14143).
Variables can be passed from an HTML page into the inserted Flash movie that it encompasses. This approach requires that tags are written into the HTML source that address both Internet Explorer and Netscape. The variable information is passed to the main timeline of the Flash movie as soon as the SWF loads. The example below uses a text field on the main timeline to display the variable that is being passed.
To pass variables on a query string to a Flash movie in HTML:
Note: Multiple variables can be passed with this syntax: "movie.swf?variable1=value1&variable2=value2". The value must be assigned in both the OBJECT and EMBED tags for this method to work in all browsers. Also, Internet Explorer on the Macintosh will not display the movie locally. To preview this sample in that browser, upload the SWF and HTML files to a server and type in the full "http://" address.
To create this sample, do the following:
Using variables in Flash can create powerful and scalable movies that can change to reflect user choices, updates and events. Writing expressions adds new levels of complexity to authoring in Flash. For more instructions on many Flash scripting and design techniques, please visit the Flash Support Center and view related TechNotes. Enter keyword topics into the search field to access a wide variety of articles on beginning and advanced topics.