Game Development Reference
In-Depth Information
As usual, we have a camera and a
SpriteBatcher
to render our UI elements and the background.
To check whether a touch event hit a button, we store a
Vector2
instance and four
Rectangle
instances for the four buttons onscreen.
public
SettingsScreen(Game game) {
super
(game);
guiCam =
new
Camera2D(glGraphics, 480, 320);
batcher =
new
SpriteBatcher(glGraphics, 10);
touchPoint =
new
Vector2();
touchBounds =
new
Rectangle(120-32, 160-32, 64, 64);
accelBounds =
new
Rectangle(240-32, 160-32, 64, 64);
soundBounds =
new
Rectangle(360-32, 160-32, 64, 64);
backBounds =
new
Rectangle(32, 32, 64, 64);
}
In the constructor, we set up all the members for the screen. No rocket science involved here.
@Override
public void
update(
float
deltaTime) {
List<TouchEvent> events = game.getInput().getTouchEvents();
int
len = events.size();
for
(
int
i = 0; i < len; i++) {
TouchEvent event = events.get(i);
if
(event.type != TouchEvent.
TOUCH_UP
)
continue
;
guiCam.touchToWorld(touchPoint.set(event.x, event.y));
if
(OverlapTester.
pointInRectangle
(touchBounds, touchPoint)) {
Assets.
playSound
(Assets.
clickSound
);
Settings.
touchEnabled
=
true
;
Settings.
save
(game.getFileIO());
}
if
(OverlapTester.
pointInRectangle
(accelBounds, touchPoint)) {
Assets.
playSound
(Assets.
clickSound
);
Settings.
touchEnabled
=
false
;
Settings.
save
(game.getFileIO());
}
if
(OverlapTester.
pointInRectangle
(soundBounds, touchPoint)) {
Assets.
playSound
(Assets.
clickSound
);
Settings.
soundEnabled
= !Settings.
soundEnabled
;
if
(Settings.
soundEnabled
) {
Assets.
music
.play();
}
else
{
Assets.
music
.pause();
}
Settings.
save
(game.getFileIO());
}