Coding the Bat input handling
You probably remember we saw the onTouchEvent
method in the Sub' Hunter project. It was provided by the Activity
class. In this project, however, the input handling is not in the Activity
class. If it were we would need to somehow share values between PongActivity
and PongGame
and things might get into a bit of a muddle. Fortunately, the onTouchEvent
method is also provided by SurfaceView
which PongGame
extends (inherits from).
This time we will make our code a little bit more advanced to handle left, right and stop as well as to trigger setting mPaused
to false and start the game.
Add all the code at once and then we will dissect it and discuss how it works. Be sure to read the comments.
// Handle all the screen touches ...
Get Learning Java by Building Android Games - Second Edition 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.