Game Development Reference
In-Depth Information
We will now move on to Canyon Bunny and implement alternative input controls
using the accelerometer. The screen needs to be rotated by 90 degrees to the left in
order to be able to play Canyon Bunny on a smartphone, since it is using the so-
called landscape mode for display. It is important to understand that the orientation
of the sensors will always remain the same no matter how the smartphone is moved
and rotated around.
The new controls will allow you to tilt the screen to the left and right sides to
move into the very same direction. The tilt angle for the y axis will determine the
maximum velocity of the player character.
Add the following new constants to the Constants class:
// Angle of rotation for dead zone (no movement)
public static final float ACCEL_ANGLE_DEAD_ZONE = 5.0f;
// Max angle of rotation needed to gain max movement velocity
public static final float ACCEL_MAX_ANGLE_MAX_MOVEMENT = 20.0f;
Then, add the line of code to the WorldController class:
import com.badlogic.gdx.Input.Peripheral;
Next, add the following line to the WorldController class:
private boolean accelerometerAvailable;
After this, make the following changes to the same class:
private void init () {
accelerometerAvailable = Gdx.input.isPeripheralAvailable(
Peripheral.Accelerometer);
cameraHelper = new CameraHelper();
lives = Constants.LIVES_START;
livesVisual = lives;
timeLeftGameOverDelay = 0;
initLevel();
}
private void handleInputGame (float deltaTime) {
if (cameraHelper.hasTarget(level.bunnyHead)) {
// Player Movement
if (Gdx.input.isKeyPressed(Keys.LEFT)) {
...
} else {
// Use accelerometer for movement if available
 
Search WWH ::




Custom Search