|
|||
| week 08b - proj 3, keyboard, collision detection, move clips | |||
keyboard input : |
use the keyboard for interactions References: Textbook pgs. 555-567, also Flash Help: ActionScript Dictionary, Key (object) So far, we've been using the mouse as the primary way to interact with the application. But the keyboard is also a way to for the user to interacti with an application. Many games use the keyboard to take advantage people's hand/eye coordination, which is severely blunted by the blunt tool character of the mouse. In particular, the keyboard is useful when there are many different kinds of interactions available to the user, where different fingers and both hands can access functions on different keys. As a simple example, this Flash movie uses the left, right, up & down cursor keys to move the graphic in the corresponding directions, while the space bar rotates the graphic. To make sure all the key events are captured by the Flash movie and not the browser, click on the Flash movie first.
The code for this is straight-forward, and simply checks to see if a key is currently depressed. You can place the keyboard scanning software in an enterFrame clip event handler within the movieClip. The code for moving left and right is below, and the other keys are captured in a similar manner.
The Key.isDown(keyCode) method checks to see the if key specified by keyCode is currently held down, and returns TRUE if it is, FALSE if not. Flash includes several predefined key constants as properties of the Key object. In the above example, the if conditions test the result of the Key.isDown() test with the Key.RIGHT (right cursor key) and the Key.LEFT (left cursor key) constants. If the corresponding keys are held down, the movieClip is moved in the appropriate direction by adding/subtracting pixels. Use this code example and the table of Key constants below to re-create all the functions in the above Flash movie.
|
|||||||||||||||||||||||||||||||||||||||
| Key codes : | Most of the non-alphabetic keys are available as constants defined in the Key object (e.g. Key.ENTER), as described in the table below. Other keys on the keyboard must be identified by their keycode. These codes are: A-Z = 65 to 90 A complete list of keycodes is in the Flash help, Using Flash: Keyboard Keys and Key Code Values. The following code is a way to get the keycode for any key, by simply pressing the key:
From Flash Help: ActionScript Dictionary, Key (object), standard keycode constants:
|
| all materials on this web site © copyright 2003, Philip van Allen |
top |