Introduction to ActionScript Tutorial > Save and retrieve information > Declare a variable and assign it a value

 

Declare a variable and assign it a value

When you need a variable, you must name it, or declare it. You must also assign it a value. You can either do both things at once, or you may declare a variable in one statement and then assign it a value in a later statement.

ActionScript uses three types of variables: local variables, global variables, and Timeline variables. You can use the var action inside a block of code (designated by curly brackets {}) to create a local variable, which disappears when the code block finishes running. You can use the set variable action to create a Timeline variable attached to the Timeline of a movie clip, which can be used in any script in the document. For more information about variables, see Understanding the ActionScript Language.

The puzzle.fla file uses the var action and the set variable action depending on the situation. When a variable is only needed within a block of code, the var action is used. The dialog variable is set and assigned using the set variable action.

Now you'll declare and assign a value to the dialog variable:

1

Select File > Open and choose the version of mypuzzle.fla that you last saved.

Note: You can also browse to your Flash MX application folder and open Tutorials/ActionScript/Finished/puzzle2.fla. If you do use the puzzle2.fla file, save the file with a new name in your My_Puzzle folder to maintain an unadulterated version of the original file.

2

Select Frame 1 of the Actions layer. If the Actions panel isn't open, choose Window > Actions.

3

In the Script pane of the Actions panel, select line 11, which is the last line of code in the Initialize section. In the Actions toolbox, choose Actions > Variables and double-click the set variable action.

4

Type dialog in the Variable text box.

5

Type true in the Value text box. Select Expression, to the right of the Value text box.

By selecting Expression, you are telling Flash that true is not a literal string of characters.

In the movie's initial state, a dialog box is visible on the Stage. Therefore, the dialog variable must be set to true at the start of the movie—otherwise, a user can move the puzzle pieces before they are scrambled.

6

Choose File > Save As and enter a new filename. Use a consecutive naming scheme so you can revert to earlier versions of the file, if necessary.