Game Development Reference
In-Depth Information
@Override
public boolean keyUp (int keycode) {
// Reset game world
if (keycode == Keys.R) {
init();
Gdx.app.debug(TAG, "Game world resetted");
}
// Select next sprite
else if (keycode == Keys.SPACE) {
selectedSprite = (selectedSprite + 1) % testSprites.length;
// Update camera's target to follow the currently
// selected sprite
if (cameraHelper.hasTarget()) {
cameraHelper.setTarget(testSprites[selectedSprite]);
}
Gdx.app.debug(TAG, "Sprite #" + selectedSprite + "
selected");
}
// Toggle camera follow
else if (keycode == Keys.ENTER) {
cameraHelper.setTarget(cameraHelper.hasTarget() ? null :
testSprites[selectedSprite]);
Gdx.app.debug(TAG, "Camera follow enabled: " +
cameraHelper.hasTarget());
}
return false;
}
The
WorldController
class now has an instance of
CameraHelper
that is initialized
in
init()
and appended at the end of
update()
. Remember to continuously
call
update()
off.
CameraHelper
on every update cycle to ensure that its internal
calculations are also performed. In the
keyUp()
method, we add two new
functionalities. The first one is that the target of the camera helper is updated
according to a newly selected sprite. Secondly, when the
Enter
key is pressed, the
target is toggled on and off. Additionally, add the following code to
WorldRenderer
:
public void renderTestObjects () {
worldController.cameraHelper.applyTo(camera);
batch.setProjectionMatrix(camera.combined);
batch.begin();
for(Sprite sprite : worldController.testSprites) {
sprite.draw(batch);
}
batch.end();
}