Book Image

Android Game Programming by Example

By : John Horton
Book Image

Android Game Programming by Example

By: John Horton

Overview of this book

Table of Contents (18 chapters)
Android Game Programming by Example
About the Author
About the Reviewers
Platformer – Guns, Life, Money, and the Enemy

Player input

First, let's add some methods in the Player class that our input controller will be able to call, then manipulate the variables that the Player class's update method uses to move around.

We already played with the isPressingRight variable, and also have an isPressingLeft variable. Furthermore, we want to be able to jump. If you take a look at the Player class's update method, we already have the code to handle these situations. We just need the player to be able to initiate the movements via touches to the screen.

Our previous button layout design and the code we have written so far, suggests a method for going left, a method for going right, and a method for jumping.

You will also note that we pass a copy of SoundManager into the startJump method, which allows us to play a neat retro jumping sound, if the jump attempt is successful. Add these three new methods to the Player class:

public void setPressingRight(boolean isPressingRight) {
        this.isPressingRight = isPressingRight...