Installing Key Listeners

[ LiB ]

A key listener is a method that executes its block of code after an event happens no matter what point you are in the program. What this means is that a key listener is a special type of event handler. So what does it exactly do? It responds when a key is pressed and released.

See Figure 8.4 and observe what happens when a key is pressed and released.

Figure 8.4. onKeyDown handler being called after a key press

graphic/08fig04.gif


When would you want to use a key listener instead of the isDown method you learned about in the last section? A key listener is useful when your program is running on a slow computer and your game requires you to process every key strokedetecting all key strokes can be easily done using key listeners. Now, consider the same program running on the same slow computer. If this game is using the isDown method to test for a key press, problems will arise. The code for this test runs only once every frame, and if it is a slow computer, there is a possibility the key press won't be detected in between frames . You won't have to worry about this situation if you use key listeners.

Open GDA_PROG8.5.fla, and then view the following listing.

 // Game Development with ActionScript // By Lewis Moronta (c) 2003 // This program demonstrates how to // install the Key object's key listener. // Assign the event handlers to the // _root object (your main timeline/movie) Key.addListener(_root); // Now that you have installed the // handler, you can define the two handlers. onKeyUp = function () {   trace("You have released a key."); }; onKeyDown = function () {   trace ("You have pressed a key."); }; 

Figure 8.5 shows the output I got when testing the code. Notice that the program will stop whatever it's doing to execute these key listeners.

Figure 8.5. Output from the key event handlers

graphic/08fig05.gif


Before continuing, go ahead and play with this program. Hold down a key, then release it. Hold down multiple keys, and release. Make sure you understand what's happening.

The addListener method accepts an object as its parameter. The object that is passed to it depends on where the onKeyUp and onKeyDown handlers will be installedall you have to do is define the handlers.

NOTE

NOTE

Note that I used the _root object as our target.You could've used any objecteven custom objects and classes. In place of _root , I could've used the this keyword because I placed the code on the main time line.

Defining the new handlers isn't very much different from defining the onEnterFrame handler. Check out how I did it:

 onKeyUp = function () {   trace("You have released a key."); }; 

As you can see, all you have to do is assign the function block to the onKeyUp alias.

The block itself consists of a single trace command that tells us that a key was released. This happens because the onKeyUp handler is called when a key is released.

The following handler, onKeyDown , is very similar to the onKeyUp handler:

 onKeyDown = function () {   trace ("You have pressed a key."); }; 

The onKeyDown handler responds and executes its block as soon as any key is pressed.

[ LiB ]


Game Development with ActionScript
Game Development with ActionScript
ISBN: 1592001106
EAN: 2147483647
Year: 2004
Pages: 162
Authors: Lewis Moronta

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