96 Chapter 5: Creating Interaction with ActionScript
To get the current mouse position within a movie clip:
1 Create a movie clip.
2 Select the movie clip instance on the Stage. Using the Property inspector, name it myMovieClip.
3 Select Window > Development Panels > Actions to open the Actions panel if it is not
already visible.
4 Use the movie clip’s instance name to return the mouse position within the main Timeline.
For example, the following statement could be placed on any Timeline in the
_level0 SWF
file to return the
_ymouse position in the myMovieClip instance:
x_pos = _root.myMovieClip._xmouse
y_pos = _root.myMovieClip._ymouse
The code returns the _xpos and _ypos of the mouse, relative to the registration point.
5 Select Control > Test Movie to test the movie.
You can also determine the mouse position within a movie clip by using the
_xmouse and
_ymouse properties in a clip event, as shown in the following code:
onClipEvent(enterFrame){
xmousePosition = this._xmouse;
ymousePosition = this._ymouse;
}
For more information about the _xmouse and _ymouse properties, see MovieClip._xmouse
on page 541 and
MovieClip._ymouse on page 543.
Capturing keypresses
You can use the methods of the built-in Key class to detect the last key pressed by the user. The
Key class does not require a constructor function; to use its methods, you simply call the methods
on the class itself, as shown in the following example:
Key.getCode();
You can obtain either virtual key codes or ASCII (American Standard Code for Information
Interchange) values of keypresses:
• To obtain the virtual key code of the last key pressed, use the getCode() method.
• To obtain the ASCII value of the last key pressed, use the getAscii() method.
A virtual key code is assigned to every physical key on a keyboard. For example, the Left Arrow
key has the virtual key code 37. By using a virtual key code, you ensure that your SWF file’s
controls are the same on every keyboard, regardless of language or platform.
ASCII values are assigned to the first 127 characters in every character set. ASCII values provide
information about a character on the screen. For example, the letter “A” and the letter “a” have
different ASCII values.
To decide which keys to use and determine their virtual key codes, use one of these approaches:
• See the list of key codes in Appendix C, “Keyboard Keys and Key Code Values,” on page 789.
• Use a Key class constant. (In the Actions toolbox, click the Built-in Classes category, click
Movie, click Key, and click Constants.)