Chapter    8

Reading User Input

If you have never coded a game for a mobile device or tablet, one problem will quickly arise. Unlike for gaming consoles, or even desktops, a distinct lack of input options exist to relay your player’s intentions back into the game code. Without the benefit of game controllers, keyboards, or mice, supplying your player with a complex input system can be difficult.

The majority of your player input will come from the device’s touch screen. Wiring up your game to detect and respond to touch events on the device is not as hard as it may appear on the surface.

Using the onTouchEvent()

Within your GameView class, override the onTouchEvent() as follows:

@Overridepublic boolean onTouchEvent(MotionEvent event) {}

The ...

Get Android Studio Game Development: Concepts and Design now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.