Game Development Reference
In-Depth Information
The constructor calls the superclass constructor and creates a new
World
instance. The game
screen will be in the ready state after the constructor returns to the caller.
@Override
public void
update(
float
deltaTime) {
List < TouchEvent > touchEvents = game.getInput().getTouchEvents();
game.getInput().getKeyEvents();
if
(state == GameState.
Ready
)
updateReady(touchEvents);
if
(state == GameState.
Running
)
updateRunning(touchEvents, deltaTime);
if
(state == GameState.
Paused
)
updatePaused(touchEvents);
if
(state == GameState.
GameOver
)
updateGameOver(touchEvents);
}
Next comes the screen's
update()
method. All it does is fetch the
TouchEvent
s and
KeyEvent
s
from the input module and then delegate the update to one of the four update methods that we
implement for each state based on the current state.
private void
updateReady(List < TouchEvent > touchEvents) {
if
if(touchEvents.size() > 0)
state = GameState.
Running
;
}
The next method is called
updateReady()
. It will be called when the screen is in the ready state.
All it does is check if the screen was touched. If that's the case, it changes the state to running.
private void
updateRunning(List < TouchEvent > touchEvents,
float
deltaTime) {
int
len = touchEvents.size();
for
(
int
i = 0; i < len; i++) {
TouchEvent event = touchEvents.get(i);
if
(event.type == TouchEvent.
TOUCH_UP
) {
if
if(event.x < 64 && event.y < 64) {
if
(Settings.
soundEnabled
)
Assets.
click
.play(1);
state = GameState.
Paused
;
return
;
}
}
if
(event.type == TouchEvent.
TOUCH_DOWN
) {
if
if(event.x < 64 && event.y > 416) {
world.snake.turnLeft();
}
if
(event.x > 256 && event.y > 416) {
world.snake.turnRight();
}
}
}