Game Development Reference
In-Depth Information
@Override
public void
update(
float
deltaTime) {
List < TouchEvent > touchEvents = game.getInput().getTouchEvents();
game.getInput().getKeyEvents();
int
len = touchEvents.size();
for
(
int
i = 0; i < len; i++) {
TouchEvent event = touchEvents.get(i);
if
(event.type == TouchEvent.
TOUCH_UP
) {
if
(event.x < 64 && event.y > 416) {
if
(Settings.
soundEnabled
)
Assets.
click
.play(1);
game.setScreen(
new
MainMenuScreen(game));
return
;
}
}
}
}
Next, we define the
update()
method, which is unsurprisingly boring. All we do is check for
whether a touch-up event pressed the button in the bottom-left corner. If that's the case, we play
the click sound and transition back to the
MainMenuScreen
.
@Override
public void
present(
float
deltaTime) {
Graphics g = game.getGraphics();
g.drawPixmap(Assets.
background
, 0, 0);
g.drawPixmap(Assets.
mainMenu
, 64, 20, 0, 42, 196, 42);
int
y = 100;
for
(
int
i = 0; i < 5; i++) {
drawText(g, lines[i], 20, y);
y += 50;
}
g.drawPixmap(Assets.
buttons
, 0, 416, 64, 64, 64, 64);
}
The
present()
method is pretty simple, with the help of the mighty
drawText()
method
we previously defined. We render the background image first, as usual, followed by the
“HIGHSCORES� portion of the
Assets.mainmenu
image. We could have stored this in a separate
file, but we reuse it to free up more memory.
Next, we loop through the five strings for each high-score line we created in the constructor.
We draw each line with the
drawText()
method. The first line starts at (20,100), the next line is
rendered at (20,150), and so on. We just increase the y coordinate for text rendering by 50 pixels
for each line so that we have a nice vertical spacing between the lines. We finish the method off
by drawing our button.