Java Reference
In-Depth Information
Table 8-2.
The Various Transformations You Can Apply to a Sprite's Frames
Constant
Action
Sprite.TRANS_NONE
No translation
Sprite.TRANS_ROT180
Rotate 180° about reference pixel
Sprite.TRANS_MIRROR
Horizontal mirror about the reference pixel
Sprite.TRANS_MIRROR_ROT180
Rotate 180° about reference pixel, then horizontal mirror
about reference pixel
Sprite.TRANS_ROT90
Rotate 90° clockwise about reference pixel
Sprite.TRANS_MIRROR_ROT90
Horizontal mirror about the reference pixel, then rotate 90°
clockwise
Sprite.TRANS_MIRROR_ROT270
Horizontal mirror about the reference pixel, then rotate 90°
counterclockwise
Sprite.ROT270
Rotate 90° counterclockwise about reference pixel
Finally, the
Sprite
class provides collision detection using the
collidesWith
method,
checking to see if two
Sprite
instances (or a
Sprite
and an
Image
) collide. The
Sprite
class
can run this collision test using either the rectangle of the frame image or the opaque
pixels in the frame (which is a trifle slower); simply pass
true
if you want pixel-by-pixel
collision detection. Using the
collidesWith
method can simplify your game, as you don't
need to perform your own collision detection.
Putting the Mobile Game API to Work
Although writing a full game is beyond the scope of this chapter, it's instructive to see
all of these pieces together in a full example. Figure 8-4 shows a simple game using the
Mobile Game API that lets you move a cat about with the directional pad, chasing the
moving butterflies. When the cat touches a butterfly, the handset's backlight flashes and
the handset vibrates.
The application has two classes: one is responsible for implementing the MIDlet
interface, and the other is a
GameCanvas
subclass that implements the game behavior
itself. In addition, the MIDlet contains the artwork for the game, as shown in Listing 8-9.