Introduction to ActionScript Tutorial > Use a built-in object

 

Use a built-in object

ActionScript has variables that let you store information; it has functions that let you create special commands and reuse code; it has actions that let you control the flow of a movie; and it has movie clips with properties that you can change.

ActionScript also has another type of element called a built-in object. Objects provide a way of grouping information so that you can use it in a script. Objects can have properties, methods (which are like functions), and constants (for example, the numeric value of Pi).

In the RotateDisplayOrDrag function that you created in Create commands and reuse code, you used the Key object to determine the last key a user pressed on the keyboard. The Key object is built into ActionScript to allow you to access information about the keyboard.

Another ActionScript object is the MovieClip object. The MovieClip object is a collection of methods that you can use to manipulate movie clips, which are the most fundamental and powerful elements of Flash. To learn more about the special characteristics of the MovieClip object and using movie clips, see Working with Movie Clips and Buttons.

You will now use the onPress method of the MovieClip object to check whether the mouse pointer is touching a puzzle piece.

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/puzzle7.fla. If you do use the puzzle7.fla file, save the file with a new name in your My_Puzzle folder to maintain an unadulterated version of the original file.

2

Piece actions is a movie clip nested within each instance of a puzzle piece. To select the Piece actions movie clip from the Library panel hierarchy, click the Edit Symbols button in the lower right corner of the Timeline and choose Misc > Piece actions.

The Piece actions movie clip opens in symbol-editing mode.

3

In the Piece actions Timeline, select Frame 1 of the Actions layer.

4

If the Actions panel is not already open, choose Window > Actions. In the Script pane, select line 3 with the following line of code:

// ENTER code here

5

In the Objects > Movie > MovieClip > Events category of the Actions toolbox, double-click the onPress action.

The onPress method is a special type of action called an event handler, or simply an event. An event allows you to write a function that runs when a certain event occurs. Types of events include when the mouse button is pressed, when the playhead enters a frame, and when a movie clip loads.

In this procedure, the code within the curly brackets that follow onPress runs when a user presses the mouse button in the movie.

6

Type _parent in the Object text box.

Because Piece actions is nested within the movie clip, _parent specifies that onPress should execute on code one level up, at the same level as the puzzle piece.

7

In the Actions > Conditions/Loops category of the Actions toolbox, double-click the if action.

8

Type !_root.dialog in the Condition text box. The code appears as follows:

_parent.onPress = function(){
	if (!_root.dialog) {
	{
};

The code that you added in this step tests whether the value of the variable named dialog is true (visible) or false (not visible). If the value is true, then the next script to rotate and drag the puzzle piece will not run. If the value of the variable is false, then the following script will run. Users can't rotate or drag a piece, or display its piece number, if the dialog box is showing.

9

In the Actions toolbox, double-click the evaluate action in the Actions > Miscellaneous Actions category to add it inside the curly brackets of the if statement.

10

Type _root.RotateDisplayOrDrag(_parent._name) in the Expression box.

_name is the property for the name of the puzzle piece instance that the user clicks.

The final code looks like this:

_parent.onPress = function(){
	if (!_root.dialog) {
	_root.RotateDisplayOrDrag(_parent._name);
	}
};

11

Do one of the following to return to the main Timeline:

Choose Edit > Edit Document.

Click the Back button.

Click Scene 1.

12

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.