Introduction to ActionScript Tutorial > Control the flow of the movie > Write a conditional statement |
![]() ![]() ![]() |
Write a conditional statement
You've already used an operator to show and hide a movie clip. Now you'll use an if
statement to create logic that shows and hides the piece numbers
movie clip. For the sake of learning, this example uses a different ActionScript element to achieve the same result.
1 |
If necessary, choose File > Open and choose the version of the mypuzzle.fla file that you last saved. |
Note: You can also browse to your Flash MX application folder and open Tutorials/ActionScript/Finished/puzzle5.fla. If you do use the puzzle5.fla file, save the file with a new name in your My_Puzzle folder to maintain an unadulterated version of the original file. |
|
2 |
On the Stage, click the Show/Hide Piece number matrix button. If the Actions panel isn't open, choose Window > Actions. In the Actions toolbox, choose the Actions > Conditions/Loops category. |
3 |
Double-click the |
on (release) { if (<not set yet>) { } } |
|
4 |
With the line of code that contains the |
The code looks like the following: |
|
on (release) { if (<not set yet>) { } else { } } |
|
5 |
Select line 3, which begins with |
_root.piecenumbers |
|
6 |
With the insertion point in the Condition text box, enter ._visible after |
7 |
From the Actions > Miscellaneous Actions category in the Actions toolbox, double-click the |
8 |
Enter _root.piecenumbers._visible = false in the Expression text box. |
You can use the Insert Target Path button or type the code manually. The code now looks like the following: |
|
on (release) { if (_root.piecenumbers._visible) { _root.piecenumbers._visible = false; } else { } } |
|
When the movie plays, Flash evaluates the expression inside the condition parentheses. The expression must equal one of the Boolean values: |
|
9 |
In the Script pane, select the line of code with the |
10 |
Enter _root.piecenumbers._visible = true in the Expression text box. |
The final ActionScript code looks like this: |
|
on (release) { if (_root.piecenumbers._visible) { _root.piecenumbers._visible = false; } else { _root.piecenumbers._visible = true; } } |
|
11 |
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. |
![]() ![]() ![]() |