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

Controlling the spaceship

We have our player's spaceship floating aimlessly on the screen starting 50 pixels from the left and 50 pixels from the top and drifting slowly to the right. Now, we can give the player the power to control the spaceship.

Remember the design for the controls is a one finger tap and hold to boost, release to quit boosting and decelerate.

Detecting touches

The SurfaceView class that we extended for our view is perfect for handling screen touches.

All we need to do is override the onTouchEvent method within our TDView class. Let's see the code in full, and then we can examine it more closely to make sure we understand what is going on. Enter this method in the TDView class and import the necessary classes in the usual way. I have highlighted the parts of the code that we will be customizing later:

// SurfaceView allows us to handle the onTouchEvent
public boolean onTouchEvent(MotionEvent motionEvent) {

    // There are many different events in MotionEvent