USING THE KEY OBJECT TO ADD INTERACTIVITY


The Key object is a useful tool for capturing key events (that is, user interaction with the keyboard) from the user. You can employ the Key object to:

  • Determine whether a specific key is currently pressed

  • Determine the last key pressed

  • Obtain the key code value of the last key pressed

  • Add a listener that watches for a key event.

  • Determine whether a certain key (like Caps Lock) is toggled on or off

The Key object is a global object, which means you cannot create a new instance of it. The most common use of the Key object is to determine whether a specific key is pressed. You would use the following syntax to determine whether a key is being pressed:

 Key.isDown(Key.TAB); 

The above line of ActionScript which uses the isDown() method of the Key object to determine whether the Space bar is currently pressed down returns a result of either true or false . The parameter of the isDown() method can reference either a specific key in the Key object or the key code of a specific key. For instance, the Tab key can be referenced using Key.TAB , or by the number 9, which is its ASCII equivalent. Thus, the following line of script is essentially the same as the previous one:

 Key.isDown(9); 

In the following simple exercise, you'll move a hot-air balloon around on the screen using your keyboard and the Key object.

  1. Open balloon1.fla in the Lesson04/Assets directory.

    This movie contains two layers: Background and Balloon. On the Balloon layer, you'll see a movie clip instance of a hot-air balloon. In this exercise, you'll place actions on that movie clip that will be executed using clip events.

    graphics/04fig08.gif

  2. With the Actions panel open, select the hot-air balloon movie clip instance and add this script:

     onClipEvent (load) {    speed = 3;  } 

    At the end of this exercise, you should be able to move the balloon with your arrow keys. Every time you press the key, the balloon should move a certain amount. When this movie clip instance loads, the script sets the value of speed , which determines how much the balloon moves every time you press a key. When used in the following scripts, this value represents a distance of 3 pixels.

  3. With the balloon instance still selected, add this script at the end of the current script:

     onClipEvent (enterFrame) {    if (key.isDown(key.RIGHT)) {      _x += speed;    } else if (key.isDown(key.LEFT)) {      _x -= speed;    }  } 

    This script uses an enterFrame event to evaluate a conditional statement. Key.RIGHT references the right arrow key, and Key.LEFT references the left arrow key. Thus, each time this statement is evaluated, the movie clip instance is moved to its current x position plus the value of speed if the right arrow key is pressed (moving the instance to the right). However, if the right arrow key is not pressed, that part of the script is ignored and the next part is evaluated which means that if the left arrow key is pressed, the movie clip instance is moved to its current x position minus the value of speed (moving the instance to the left).

    graphics/04fig09.gif

  4. Add the following ActionScript to the enterFrame clip event:

     if (key.isDown(key.UP)) {    _y -= speed;  } else if (key.isDown(key.DOWN)) {    _y += speed;  } 

    This ActionScript is similar to the ActionScript we added in the previous step. However, here the if statement reacts to the up and down arrow keys (rather than the right and left arrow keys). The first part of this statement says that if the up arrow key is pressed, the movie clip instance is moved to its current y position minus the value of speed (moving the instance up). If the down arrow key is pressed, the instance is moved to its current y position plus speed (moving the instance down).

  5. Choose Control > Test Movie. Use the left, right, up, and down arrow keys to control the movement of the hot-air balloon.

    The conditional statements you wrote are performed in each frame of the enterFrame clip event. If one of the keys is detected to be in the down state, the balloon will be moved accordingly.

  6. Close the test movie and save your work as balloon2.fla.

    You now know how to capture a key press and make something happen as a result. There are many applications for this, such as paging through a slide show or making a game.



Macromedia Flash MX ActionScripting Advanced. Training from the Source
Macromedia Flash MX ActionScripting: Advanced Training from the Source
ISBN: 0201770229
EAN: 2147483647
Year: 2002
Pages: 161

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net