Java Reference
In-Depth Information
case 9:
x += width/2;
y -= height/2;
break;
}
// Clip coordinates
if ( x < 0 ) x = 0;
if ( y < 0 ) y = 0;
if(x>board.getWidth() - width )
x = board.getWidth() - width;
if ( y > board.getHeight() - height )
y = board.getHeight() - height;
butterfly[i].setPosition(x,y);
butterfly[i].nextFrame();
}
}
}
Broadly speaking, the
SpriteCanvas
class methods can be broken up into three groups:
•
Game setup
: This is the responsibility of the constructor and the helper methods
createButterflies
,
createCat
, and
createBoard
.
•
Game play
: This is the responsibility of the game loop; it includes polling for
keystrokes and moving nonplayer characters.
•
Handling pause and resume events
: In the event of a state change (say, due to an
incoming call), the
SpriteCanvas
must pause game play. The MIDlet triggers this by
invoking the
setPaused
method.
The constructor begins by invoking the
GameCanvas
constructor, indicating that this
GameCanvas
subclass will poll for keystrokes rather than receive keystroke events. Next, it
caches aside the display its creator gives it, and it creates a new instance of
Random
used by
the nonplayer characters. Finally, it creates and initializes the
LayerManager
(the skulldug-
gery with the view window ensures that the game board will be centered in the display)
and the various layers shown by the game. Note that the method creates the various lay-
ers in Z-order, with the butterflies closest to the user; this is because each creation
routine adds the resulting
Layer
to the
LayerManager
.