Java Reference
In-Depth Information
The createBoard method creates an Image containing the tiles used to create the game
background, and then it creates a TiledLayer , setting the cells in the TiledLayer to the tiles
indicated by map. In a real game, this would be more complex, invoking a level loader that
would load a specific game level from the game's resources, but the principle is essentially
the same. Once the code initializes the TiledLayer , it adds it to the LayerManager .
The createButterflies method creates two butterflies by first loading the frame Image
used in the butterflies' animation and then creating each butterfly Sprite in turn. For
each butterfly Sprite , the code must select a random starting position for the butterfly,
initialize the frame sequence (which shows how the butterfly's animation moves through
the sequence of frames) and start the butterfly at a different frame than the previous but-
terfly. Once the method creates and initializes each butterfly Sprite , it adds it to the
LayerManager . The createCat method is similar, except that the cat has no animation
frames, so it omits the process of setting the frame sequence.
The game loop—started by the start method—must manage the movement of
the cat and butterflies, detect any collisions between these characters, and redraw the
screen. The run method accomplishes this work, together with the helper methods
moveCat , moveButterflies , and detectCollisions . Finally, the game loop waits on the
paused variable if paused is true , giving the MIDlet a way to pause the game.
The moveCat method begins by polling for keystrokes and testing for the directional
keys. For each directional key pressed, the code incrementally shifts the cat's position;
when all key combinations have been tested, the code checks to ensure the cat is still on
the game board before setting the cat's position.
The moveButterflies method works similarly, with two differences. First, butterflies
move randomly; second, moveButterflies must move all butterflies. It does so using a
simple loop. Of course, this method and moveCat could be refactored to use a separate
helper function, but I chose not to in order to keep the responsibilities of each method
clear. In a more sophisticated game, this behavior might well be delegated to whole
classes implementing the behavior of characters.
The code for detectCollisions delegates detecting collisions to the Sprite cat using
its collidesWith method and passing each butterfly Sprite in turn. When cat detects a
collision, the code uses the Display instance passed to the canvas's constructor to flash
the backlight and vibrate the handset.
The game's main loop manages game pause and resume actions through the
setPaused mutator, its associated paused variable, and Java's thread synchronization.
When the member variable paused is true , the game loop in the SpriteCanvas 's run
method waits, and the thread sleeps. When the MIDlet invokes setPaused again, it trig-
gers a notify on the member variable paused , and the thread continues execution.
 
Search WWH ::




Custom Search