Game Development Reference
In-Depth Information
create a high score listing). Listing 4-9 shows two boolean variables used to check if the game is over and
a long variable used to return the score.
Listing 4-9. Getting the Game Score
protected boolean gameOver() {
return loaded && !playing;
}
@Override
protected long getScore() {
return score;
}
Responding to Key Press and Touch Events
The final piece of the puzzle is listening for user events such as key press and touch events and taking the
appropriate action. We do this by overriding the onKeyUp , onKeyDown , and onTouchEvent methods. The
following tasks are performed:
Update the class variables: up , down , left , or right depending on which arrow key
is pressed.
Fire photons when the space bar is pressed.
Jump into hyperspace when H is pressed. The ship will disappear from one point
and appear in a random location on the screen.
Toggle the pause mode when P is pressed.
Toggle sound (mute the game) when M is pressed.
Start the game when S is pressed.
Quit the game when E is pressed.
When the screen is tapped, the game will start if and only if the game resources are loaded and the
game is not playing already.
Listing 4-10. Responding to Key Press and Touch Events
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
keyReleased(event);
return true;
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
keyPressed(event);
return true;
Search WWH ::




Custom Search