User Guide
184 Chapter 6: Creating Interaction with ActionScript
The following procedure shows several ways to get the pointer position within the main Timeline
or within a movie clip.
To get the current pointer position:
1.
Create two dynamic text fields, and name them box1_txt and box2_txt.
2.
Add labels for the text boxes: X position and Y position, respectively.
3.
Select Window > Development Panels > Actions to open the Actions panel if it is not already
open.
4.
Add only one of the following blocks of code to the script pane:
var mouseListener:Object = new Object();
mouseListener.onMouseMove = function() {
//returns the X and Y position of the mouse
box1_txt.text = _xmouse;
box2_txt.text = _ymouse;
};
Mouse.addListener(mouseListener);
5.
Select Control > Test Movie to test the movie. The box1_txt and box2_txt fields show the
position of the pointer while you move it over the Stage.
For more information about the methods of the Color class, see the Color class entry in
ActionScript Dictionary Help.. For more information about the
_xmouse and _ymouse
properties, see
MovieClip._xmouse and MovieClip._ymouse in ActionScript Dictionary Help.
Capturing keypresses
For more information about the methods of the Color class, see the Color class entry in
ActionScript Dictionary Help.See the
keyPress parameter of the on() handler entry in
ActionScript Language Reference Help.
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, as shown in the following example:
Key.getCode();
You can obtain either virtual key codes or American Standard Code for Information Interchange
(ASCII) 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.