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
@Override
public boolean onTouchEvent(MotionEvent motionEvent) {
// This switch block replaces the
// if statement from the Sub Hunter game
switch (motionEvent.getAction() &
MotionEvent.ACTION_MASK) {
//...