Friday, March 13, 2009

CHAPTER 2 - Control the flow ofthe movie

Without ActionScript, a movie plays from Frame 1 to the last frame and either repeats from Frame 1 or stops. You can use ActionScript to control the progression of a movie more precisely; you can also use it to give the user control. For example, you could place an action in Frame 5 that stops the movie until a user presses a Play button. This is a simple example of controlling the flow of a movie.

You can use the if, else, and else if actions (also called statements) to create a more complex movie flow called logic. These three actions perform the following tasks:

• The if action lets Flash check a condition in the movie and run certain actions if that condition is true.
• The else statement tells Flash to run a different set of actions if the if condition is false.
• The else if statement lets Flash check for another condition before running yet a different set of actions.

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 () { }
}

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 () { } 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.
.
.

==============================================
.
Contents
Trademarks
CHAPTER 1 - Introduction to Flash MX Tutorial
CHAPTER 1 - What you should know
CHAPTER 1 - View the completed movie
CHAPTER 1 - Analyze the stiletto.fla file
CHAPTER 1 - Define properties for a new document and create a ...
CHAPTER 1 - Create and mask vector art
CHAPTER 1 - Tween bitmap effects within a movie cl...
CHAPTER 1 - Load dynamic text at runtime
CHAPTER 1 - Add animation and navigation to button...
CHAPTER 1 - Add streaming and event sounds
CHAPTER 1 - Organize your Library panel
CHAPTER 1 - Test download performance and publish ...
CHAPTER 1 - The next steps
.
CHAPTER 2 - Introduction to ActionScript Tutorial
CHAPTER 2 - View a completed movie
CHAPTER 2 - Initialize the movie
CHAPTER 2 - Save and retrieve information
CHAPTER 2 - Display information in a dynamic text ...
CHAPTER 2 - Write an expression
CHAPTER 2 - Control the flow ofthe movie
CHAPTER 2 - Create commands and reuse code
CHAPTER 2 - Use a built-in object
CHAPTER 2 - Test the movie
CHAPTER 2 - The next steps
.
CHAPTER 3 - Introduction to Components Tutorial
CHAPTER 3 - Types of components
CHAPTER 3 - View the completed form
CHAPTER 3 - Create a form
CHAPTER 3 - The next steps
.
==============================================