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 if action. The following code appears in the Actions panel:

on (release) {
    if (<not set yet>) {
    }
}

4

With the line of code that contains the if action selected, double-click the else action in the Actions toolbox.

The code looks like the following:

on (release) {
    if (<not set yet>) {
    } else {
    }
}

5

Select line 3, which begins with if, and with the insertion point in the Condition text box, click the Insert Target Path button. Select the piecenumbers movie clip, Dots, and Absolute, and click OK. The following code appears in the Condition text box:

_root.piecenumbers

6

With the insertion point in the Condition text box, enter ._visible after _root.piecenumbers.

7

From the Actions > Miscellaneous Actions category in the Actions toolbox, double-click the evaluate action to add an empty line of code.

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: true or false. This example uses the condition of the if action to check if the piecenumbers movie clip is visible on the Stage. If the _visible property is true, ActionScript runs the code inside the curly brackets and sets the _visible property to false, which hides the movie clip on the Stage.

9

In the Script pane, select the line of code with the else action and double-click the evaluate action.

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.