Using the Key Object

[ LiB ]

ActionScript has a special built-in object that doesn't need a constructor to be used. You can just go right ahead and use it. The object I am referring to is called the Key object, and it represents the user 's standard keyboard. The Key object can detect key presses and spit back codes. It even has a special built-in set of predefined property constants that can help you in your game venture.

Take a look at our next demo, GDA_PROG8.3.fla, in Figure 8.2. You can see that this program seems to respond when the user presses the Up key.

Figure 8.2. Using the Key object to detect keys

graphic/08fig02.gif


You will like the fact that you won't have to use buttons to detect your key pressesthe buttons can get in the way of your graphic presentation. You can use a simple method to check whether the key has been pressed or not:

 Key.isDown(); 

This function takes the predefined constants defined for the Key object as a parameter. If you wanted to check if the Up key is being pressed, all you would have to do is write:

 Key.isDown(Key.UP); 

And this would return either true or falsetrue if the Up key were being pressed and false if it wasn't.

Since the isDown method returns a Boolean value, you can only put it to use if you use the method in an if statement as shown below.

 if (Key.isDown(Key.UP)) { } 

Now, how would you check to see whether the key is being pressed every frame? That should be simple to answerremember our old friend, onEnterFrame? If you place this if statement in an onEnterFrame handler, you would be able to literally check for this button press all throughout the program. Cool!

Go ahead and open GDA_PROG8.3.fla. Then check out the following listing.

 onEnterFrame = function () {   if (Key.isDown(Key.UP))     trace("You have pressed up."); }; 

As you examine this listing, notice that I included everything that we just discussed. I have included an if statement that checks to see if the Up key is being pressed. If it is, Flash outputs the message "You have pressed up." to the output window. This condition is checked on every frame because it is within the onEnterFrame handler.

How would you handle a regular key press? The isDown method accepts the key code as its parameter, so you can't just type in the character within quotes; if you did, that would mean you were passing the ASCII code.

NOTE

TIP

Don't be confused by all this code talkthere are various codes that are used for information interchange and a standard is the ASCII code. The isDown function only accepts ASCII codessee the appendix for a list of these codes.

So how do we resolve this problem? Lucky for us, there is another built-in method called getCode . Let's see how getCode is used:

 Key.getCode(); 

It seems as though there is no parameter for this method. The method returns a value, and that value is the ASCII code of the last key pressed.

I prepared the following listing for you to play around with. It belongs with demo GDA_PROG8.4.fla.

 onEnterFrame = function () {   display = Key.getCode(); }; 

The listing lacks comments, but I'll explain everything. First check out Figure 8.3.

Figure 8.3. Detecting scan codes

graphic/08fig03.gif


I have set up a dynamic text box. In the Properties panel, I assigned a variable to it, display . Then I selected Dynamic from the drop-down box. Once you have these settings, you can adjust what is displayed in the box by modifying the variable's contents.

As this is an onEnterFrame function, it will be executed on every frame. The display variable is being assigned the last ASCII code used. And where is this ASCII code being extracted from? It is being returned by the Key.getCode method.

The following is a list of the built-in constants that represent their respective ASCII codes:

 Key.BACKSPACE Key.CAPSLOCK Key.CONTROL Key.DELETEKEY Key.DOWN Key.END Key.ENTER Key.ESCAPE Key.HOME Key.INSERT Key.LEFT Key.PGUP Key.PGDN Key.RIGHT Key.SHIFT Key.SPACE Key.TAB Key.UP 

Now that you learned how to use the Key object to detect key presses, you can now move on to the next section where you will learn how to install key listeners.

[ 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