Game Development Reference
In-Depth Information
Okay, so now that you know how this code is working, why is it working so badly? When you tested the
program, you might have noticed a few big problems:
The movement of the lh]uan object is jittery.
When you press one of the arrow keys, there's a slight delay before the lh]uan object starts
You can move the lh]uan object in only one axis (t or u) at a time. You can move the lh]uan
object left and right, or up and down, but not diagonally. Try pressing both the up arrow and
the left arrow at the same time; it just moves the lh]uan object in the direction of whichever
key you pressed last. How can you move the lh]uan object on the diagonal?
These problems are due to your computer keyboard's key buffer . When you press a key on the
keyboard, the keyboard tells the computer that the key has been pressed down only once; it doesn't
know whether the key is being held down . The computer's operating system has a key repeat feature
built into it that resends the GAU[@KSJ event at regular intervals, however. The key repeat is needed
for word processors, for example, so that you can hold down a key to repeat a character on the
screen. You don't have any control over how the key repeat runs, and the result with a Flash game is
the jittery movement you see on the stage.
To solve this problem, you need to work around the key buffer so the keys don't directly move the
object. You can use the keys to determine the object's direction and speed, but you need to find
another way to actually move the object.
Controlling the keyboard—the right way!
Now that you know how AS3.0 can find out which keys you're pressing, you can use this same basic
system to refine the keyboard control program.
There's a lot of new stuff here, so don't panic! I'll break everything down step by step once you have
the program running to show you exactly how it works. But for now, here's a quick summary of what
you need to do:
You have to import AS3.0's Arajp class so that you can use its AJPAN[BN=IA property.
You have to create two new variables: rt and ru. These variables will store the vertical and
horizontal velocities of the object.
You'll change the kjGau@ksj event handler so it no longer changes the position of the lh]uan
object. Instead, it updates the rt and ru variables with the speed and direction that the object
should move in.
You have to add an event handler called kjGauQl. Its job is to detect when the arrow keys are
released. When they are, it sets the lh]uan object's speed to ,.
You'll create a new event handler called kjAjpanBn]ia that uses the rt and ru variables to
actually move the lh]uan.
Finally, you'll set the FLA file's frame rate to 30 frames per second so the lh]uan object will
move smoothly.
Search Nedrilad ::

Custom Search