Showcase and discover digital art at yex

Follow Design Stacks

Subscribe to our free newsletter to get all our latest tutorials and articles delivered directly to your inbox!

Different approaches to setting variables in a Flash movie

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”.

Set Variable
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”:

1 Open a new document in Flash.
2 Highlight Frame 1 in the timeline.
3 Choose Modify > Frame from the menu.

In the Actions tab, click the “+” sign, and choose “Set Variable”.


In the “Variable” field, type “text” without quotes. This is the variable name.


In the “Value” field, type “hello” without quotes.

7 Click OK.

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, i n 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.

 Use a Text Field
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.

To set a variable using a text field:

1 Open a new document in Flash.
2 Select the text tool.
3 Click the Text Field Modifier. (See image below)

Using this tool, click and drag to create a box to the desired width and height of the text field .

5 Choose Modify > Text Field from the menu, to access the Text Field Properties dialog box.

In the field marked “Variable”, type “text” (without quotes). Click OK.

7 Choose Control > Test Movie. Any value entered into the text field will become the value of “text”.

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”.)

 Load Variables
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:

1 Create a text file (using SimpleText or Notepad) containing only the text “text=hello” without quotes. Save this text file as “text.txt” to your working folder.
2 Open a new document in Flash.

Highlight Frame 1 in the timeline.

4 Choose Modify > Frame from the menu.

In the Actions tab, click the “+” sign, and choose “Load/Unload Movie” from the pop-up menu.


On the right, choose the” Load Variables into Location” radio button.


In the “URL” field, type the name of the text file “text.txt”


In the “Level” field, type the number 0. Click OK.

9 Create a text field on the stage with variable name “text”. This will demonstrate when the data has been loaded.

Publish the movie to the same folder as the text file “text.txt”.

11 When the movie plays Frame 1, the movie will pull the variable and value pair “text=hello” from your data source into the current timeline. Additional data can be added to this by using the syntax specified on page 183.

Note: Special characters (such as punctuation, numeric operators, and other non-alphanumeric data) must be URL encoded to be translated into the Flash movie.

 On a Query String in HTML tags
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:

1 Create a new Flash movie and save the file with the name “movie.fla”. Create a text field with variable name “text”.
2 Publish both Flash (movie.swf) and HTML (movie.html) files.

Open the movie.html file with a text editor such as SimpleText or Notepad.


Find the OBJECT tag. Look for this tag:

<PARAM NAME=movie VALUE="movie.swf">


At the end of “movie.swf”, add “?text=hello”

It should look like this:

<PARAM NAME=movie VALUE= "movie.swf?


Find the EMBED tag. Look for this:



Again, replace the filename “movie.swf” with “movie.swf?text=hello”.

It should look like this:



Save the file as “movie.html”, replacing the old file.


When you open the HTML file in a browser, the text field “text” in the Flash movie should display the value “hello”.

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.

 Using JavaScript
It is possible to pass variables to a Flash movie from JavaScript in the HTML page. This is just one example of a strategy that can be used in many ways. In the example below, a link in the HTML page passes a variable when clicked to a text field in the Flash movie. This method will not work on Internet Explorer for the Macintosh.

To create this sample, do the following:

1 Open a new Flash document.
2 Create a text field with variable name “text”.
3 Add an additional frame to the Flash movie so that it has at least two frames.
4 Save the document as “movie.fla” and publish your document (using the “Default” HTML template) to create the Flash movie (“movie.swf”) and HTML page (“movie.html”).
5 Open the “movie.html” file with a text editor such as SimpleText or Notepad.

Between the HEAD tags, insert this code. Be sure to copy it exactly:

<script language = "JavaScript">
function PassFlash(){"text", "hello");

7 In the EMBED tag, look for the HEIGHT and WIDTH parameters. Insert the following parameter (if it is not already in there):


8 After this has been added, insert the follow code directly following this:


9 Just above the closing body tag (“</body>”), insert this code:

<a href="#" onClick="PassFlash()">Pass The Variable</a>

10 Save the document as “movie.html”, replacing the old version of this file.

Open “movie.html” in a browser. To pass the variable to the movie, click the text “Pass the Variable”. The value of “text” becomes “hello”, and the text field will display this new value.