Friday, March 13, 2009

CHAPTER 2 - Save and retrieve information

To create a complex interactive Flash movie, you need a way for Flash to keep track of information and user activity: buttons that have been pressed, a user’s name, a score, or what sections a user has visited. ActionScript uses variables to store pieces of information that you can retrieve and use again. You can declare a variable in a script on any Timeline and use it in any other Timeline in the same movie. You must write a target path to a variable in order to use the variable in a script, just as you must write a target path to use a movie clip in a script.

In the puzzle.fla file, ActionScript uses the dialog variable to keep track of whether or not a dialog box is visible. When a dialog box appears, the dialog variable is set to true; when a user clicks a button on a dialog box, the dialog variable is set to false. This variable doesn’t affect the visibility of the dialog boxes themselves, it is simply a container that holds information that you can use in scripts throughout the movie. In the puzzle.fla file, if dialog is set to true, a user cannot move a puzzle piece.

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” under Help > Using Flash.

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.

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