Accessibility
 
Home > Products > Flash > Support > Using ActionScript
Flash Icon Macromedia Flash Support Center - Using ActionScript
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 built-in objects. 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). To view a list of ActionScript objects, look in the Objects folder in the Toolbox in the Actions panel.

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. If the key pressed was Shift while a piece was clicked, a dynamic text box displays the puzzle piece number; if the key pressed was Alt (Windows) or Option (Mac), the puzzle piece rotates 90 degrees. 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 the chapter "Working with Movie Clips" in the ActionScript Reference Guide.

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

1 Choose File > Open and choose the version of the MyPuzzle.fla file that you last saved.
2 Click the Symbols button in the upper right corner of the Timeline and choose Misc > Piece actions.

The Piece actions movie clip opens in symbol-editing mode.
3 Choose Window > Actions to open the Actions panel.
4 In the Actions panel, select the following line of code:
// ENTER code here
5 Double-click the onClipEvent action in the Action category. Choose the Mouse down event.
The onClipEvent action is a special type of action called an event handler, or just handler. A handler allows you to write code that runs when a certain event occurs. For example, when the mouse button is pressed, the playhead enters a frame, and a movie clip loads.
In this procedure, the code within the curly brackets that follow the onClipEvent handler runs when a user presses the mouse button in the movie.
6 Double-click the with action in the Action category in the Toolbox to add it to the script.
The with action allows you to specify a movie clip and write actions as if you were on the Timeline of that movie clip.
7 Enter whichOne in the Object box.
The whichOne variable is set within the onClipEvent(load) handler. The value of whichOne is _parent._parent . The property _parent refers to the instance name of the movie clip within which the current clip is nested. The code _parent._parent points to the movie clip two levels above the current clip. The _parent property allows you to reuse this script in each of the 50 puzzle pieces because it acts on movie clips relative to it and doesn't require you to know the instance name of each clip. For this purpose, the _parent property allows you to create relative paths.
8 Double-click the if action in the Toolbox list.
9 With the insertion point in the Condition box, click Objects in the Toolbox to open the Objects category. Then click MovieClip and double-click hitTest in the Toolbox list. The following code appears in the Condition box:
.hitTest( x, y, shapeFlag )

The hitTest method determines whether a movie clip has collided with either a specific point or another movie clip. In this example, you will use the current mouse position as the specific point in order to test whether the mouse is over a puzzle piece.
10 Remove the dot ( . ) from hitTest .
Often, you would specify an instance name before the dot of the hitTest method. However, because you used the with action to specify a movie clip, you don't need to do this.
11 Select the x in the Condition box and enter _root._xmouse. Select the y and enter _root._ymouse.
The hitTest method (just like a function) takes certain arguments. Two of these are x and y , which specify the x and y coordinates of a point. You use the _xmouse and _ymouse properties to determine the location of the mouse in the movie. If the mouse collides with the movie clip, the hitTest method returns the value true and the actions inside the if statement will run.
12 Select shapeFlag and substitute it with the value true. The code looks like this:
onClipEvent (mouseDown) {
    with (whichOne) {
        if (hitTest( _root._xmouse, _root.ymouse, true )) {
        }
    }
}
13 Place the insertion point after the parentheses that enclose the hitTest arguments and enter && _root.actions.dialog == false.
You need to check that the dialog variable is false because you don't want this function to be called if a dialog box is visible on the Stage. Players shouldn't be able to rotate or drag a piece, or display its piece number if a dialog box is showing.
14 Double-click the evaluate action in the Action category in the Toolbox list to add it inside the curly brackets of the if statement.
15 In the Expression box, enter _root.actions.RotateDisplayOrDrag(_name). The final code looks like this:
onClipEvent (mouseDown) {
    with (whichOne) {
        if (hittest(_root._xmouse, _root._ymouse, true) && 
_root.actions.dialog == false) {
            _root.actions.RotateDisplayOrDrag(_name);
        }
    }
}
16 Choose Edit > Movie to return to the main Timeline.
17 Choose File > Save As and enter a new filename. For example, if your file is called Jean_Puzzle7.fla, you could name it Jean_Puzzle8.fla. This allows you to revert to earlier versions of the file.
To Table of Contents Back to Previous document Forward to next document