Game Development Reference
In-Depth Information
break;
}
}
}
}
Arrow key handling: The professional way
If you compiled and tried the previous example, you will have noticed one thing
that is annoying. The sprite does not start to move as soon as you hit the key or
takes a second or so to change directions when you start to hold down a different
key. Frustrating! You can that see most of the Flash games out there do not have this
issue. Let's see how we can deal with this issue.
Another issue that needs to be dealt with is multiple key downs. For example, when
your player is controlling your spaceship, they may have more than one arrow key
held down furiously at the same time. The previous is too simple of an implementation
to handle the spaceship through a sea of asteroids, but does give you the basic of
keyboard handling.
So what's a better way to implement the same? The following describes one
possible implementation that takes care of the shortcomings of the previous simple
implementation. We will still retain the arrow key handling except that we do not
move the sprite in the callbacks, but we maintain the state of keys, whether they are
held down. So we need to maintain four Boolean values one for each arrow key. We
also will use a timer, and in the callback for which we examine the Boolean key state
values and move the sprite accordingly.
The following is a visualization of the mechanics:
 
Search WWH ::




Custom Search