Java Reference
In-Depth Information
repaints to be completed immediately. For a more in-depth discussion of
efficient use of graphics on Symbian OS please refer to Section 9.2.
8.3.4 Key-Stroke Detection
Games often need to detect fast key strokes in the game loop. In
order to make sure that no key strokes go undetected, the
Game-
Canvas.getKeyStates()
method is used. This returns an integer
representing the states of the keys on the phone, where each bit repre-
sents a specific key. The bit is 1 if the key is currently down or has been
pressed at least once since the last time the method was called. It is 0 if
the key is currently up and has not been pressed at all since the method
was last called. The following code snippet demonstrates the use of this:
private void processUserInput()
{
if(!gameOver)
{
int keys = getKeyStates();
if((keys & GameCanvas.FIRE_PRESSED) != 0)
{
gameEngine.startFiring();
...
}
if((keys & GameCanvas.UP_PRESSED) != 0)
{
gameEngine.upAction();
...
In addition, the constructor of the
GameCanvas
class takes a Boolean
value indicating whether to suppress key events. When true, the frame-
work does not call
keyPressed
,
keyRepeated
and
keyReleased
event handlers, which can reduce unnecessary overhead. Two points to
remember here is that it only applies to game actions and is appropri-
ate only if you want to use the
getKeyStates()
method for input
detection.
8.3.5 Sprites, Layers and Collision Detection
The game API has excellent support for sprites. A
Sprite
can be
moved, positioned, flipped, and mirrored around both the horizontal and
vertical axes or about an arbitrary point by using the
defineRefer-
encePixel()
method. A sprite can also be constructed from a single
image consisting of a set of equally sized frames which can then be
animated using the
nextFrame()
and
prevFrame()
methods.
You can also use the
collidesWith()
and
defineCollision-
Rectangle()
methods for collision detection between sprites and other
layers in the game world at either a bounding box or pixel level. Pixel-
level collision tests only check for collisions between opaque pixels,
which gives the best visual results but requires more processing.