Game Development Reference
In-Depth Information
In the rest of the constructor, we just load the button texture and create a
SpriteBatcher
instance, a
Camera2D
instance, and
TextureRegion
instance needed for rendering the button.
Finally, we create a
Vector2
instance so that we can transform real touch coordinates to the
coordinate system of the
Camera2D
instance that we use for UI rendering, just as we did in Super
Jumper in Chapter 9.
private
Vertices3 createCube() {
// same as in previous example
}
@Override
public void
resume() {
crateTexture.reload();
}
The
createCube()
and
resume()
methods are exactly the same as in the previous example, so
all the code isn't repeated here.
@Override
public void
update(
float
deltaTime) {
game.getInput().getTouchEvents();
float
x = game.getInput().getTouchX(0);
float
y = game.getInput().getTouchY(0);
guiCamera.touchToWorld(touchPos.set(x, y));
if
if(game.getInput().isTouchDown(0)) {
if
if(touchPos.x < 64 && touchPos.y < 64) {
Vector3 direction = camera.getDirection();
camera.getPosition().add(direction.mul(deltaTime));
}
else
{
if
if(lastX == -1) {
lastX = x;
lastY = y;
}
else
{
camera.rotate((x - lastX) / 10, (y - lastY) / 10);
lastX = x;
lastY = y;
}
}
}
else
{
lastX = -1;
lastY = -1;
}
}
The
update()
method is where all the swipe rotation and movement happens, based on
touch events. The first thing we do is empty the touch event buffer via a call to
Input.getTouchEvents()
. Next, we fetch the current touch coordinates for the first finger on the
screen. Note that if no finger is currently touching the screen, the methods we invoke will return
the last known position of the finger with index 0. We also transform the real touch coordinates
to the coordinate system of our 2D UI so that we can easily check whether the button in the
bottom-left corner is pressed.