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:

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.
  4. In the Actions tab, click the "+" sign, and choose "Set Variable".
  5. In the "Variable" field, type "text" without quotes. This is the variable name.
  6. In the "Value" field, type "hello" without quotes.
  7. Click OK.
  8. 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.

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

  1. Open a new document in Flash.
  2. Select the text tool.
  3. Click the Text Field Modifier. (See image below)
  4. 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.
  6. 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.
  3. Highlight Frame 1 in the timeline.
  4. Choose Modify > Frame from the menu.
  5. In the Actions tab, click the "+" sign, and choose "Load/Unload Movie" from the pop-up menu.
  6. On the right, choose the" Load Variables into Location" radio button.
  7. In the "URL" field, type the name of the text file "text.txt".
  8. 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.
  10. 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. For more information, see URL Encoding: Reading special characters from a text file (TechNote 14143).

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". (See above for steps)

  2. Publish both Flash (movie.swf) and HTML (movie.html) files.
  3. Open the movie.html file with a text editor such as SimpleText or Notepad.
  4. Find the OBJECT tag. Look for this tag:

    <PARAM NAME=movie VALUE="movie.swf">
  5. At the end of "movie.swf", add "?text=hello"
    It should look like this:
    <PARAM NAME=movie VALUE= "movie.swf?text=hello">

  6. Find the EMBED tag. Look for this:
    <EMBED href="/support/flash/ts/documents/movie.swf"

  7. Again, replace the filename "movie.swf" with "movie.swf?text=hello".
    It should look like this:
    <EMBED href="/support/flash/ts/documents/movie.swf?text=hello"

  8. Save the file as "movie.html", replacing the old file.
  9. 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". (See above for steps)

  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.
  6. Between the HEAD tags, insert this code. Be sure to copy it exactly:
    <script language = "JavaScript"><!-- function PassFlash(){     window.document.movie.SetVariable("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.
  11. 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.

Note: A complete description of JavaScript is beyond the scope of this TechNote. Complete technical documentation is available from numerous third-party JavaScript books and online resources such as Webmonkey's Programming JavaScript. If you use a JavaScript method to communicate with a Flash movie, additional logic may be necessary to make sure issues such as browser type, layers and such are considered when implementing a script.

Additional Information

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.

Keywords: value; Load; text field; HTML; tn_14253

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License  Twitter™ and Facebook posts are not covered under the terms of Creative Commons.

Legal Notices   |   Online Privacy Policy