Game Development Reference
In-Depth Information
if (keycode == Keys.R) {
init();
Gdx.app.debug(TAG, "Game world resetted");
}
// Toggle camera follow
else if (keycode == Keys.ENTER) {
cameraHelper.setTarget(cameraHelper.hasTarget()
? null: level.bunnyHead);
Gdx.app.debug(TAG, "Camera follow enabled: "
+ cameraHelper.hasTarget());
}
return false;
}
Now, we can use the
Enter
key to toggle between the player and camera controls.
What is still missing is the code that handles the input for the player's character.
Add the following lines of code to
WorldController
:
private void handleInputGame (float deltaTime) {
if (cameraHelper.hasTarget(level.bunnyHead)) {
// Player Movement
if (Gdx.input.isKeyPressed(Keys.LEFT)) {
level.bunnyHead.velocity.x =
-level.bunnyHead.terminalVelocity.x;
} else if (Gdx.input.isKeyPressed(Keys.RIGHT)) {
level.bunnyHead.velocity.x =
level.bunnyHead.terminalVelocity.x;
} else {
// Execute auto-forward movement on non-desktop platform
if (Gdx.app.getType() != ApplicationType.Desktop) {
level.bunnyHead.velocity.x =
level.bunnyHead.terminalVelocity.x;
}
}
// Bunny Jump
if (Gdx.input.isTouched() ||
Gdx.input.isKeyPressed(Keys.SPACE)) {
level.bunnyHead.setJumping(true);
} else {
level.bunnyHead.setJumping(false);
}
}
}