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) { // The player has put their finger on the screen case MotionEvent.ACTION_DOWN: // If the game was paused unpause mPaused = false; // Where did the touch happen if(motionEvent.getX() > mScreenX / 2){ // On the right hand side mBat.setMovementState(mBat.RIGHT); } else{ // On the left hand side mBat.setMovementState(mBat.LEFT); } break; // The player lifted their finger // from anywhere on screen. // It is possible to create bugs by using // multiple fingers. We will use more // complicated and robust touch handling // in later projects case MotionEvent.ACTION_UP: // Stop the bat moving mBat.setMovementState(mBat.STOPPED); break; } return true; }
The previous code is not as complicated as it might look at first glance. Let's break it down into sections.
First of all, notice that the entire logic is wrapped in a switch
statement and that there are two possible cases ACTION_DOWN
and ACTION_UP
. Previously we only dealt with ACTION_UP
. As a refresher, ACTION_DOWN
triggers when the finger makes contact with the screen and ACTION_UP
triggers when the finger leaves the screen.
In the first case
, ACTION_DOWN
, mPaused
is set to false
. This means that any screen touch at all will cause the game to start playing- if it isn't already. Then there is an if
-else
block.
The if
part detects if the press is on the right-hand side of the screen, (motionEvent.getX() > mScreenX / 2
). The else
, therefore, will execute if the press is on the left-hand side of the screen.
The code inside the respective if
and else
blocks are very simple, just one line. The mBat
instance is used to call its setMovementState
method to set its internal variable that will determine which direction it moves next time mBat.update
is called.
This state will remain unchanged until our code changes it. Let's see how it can be changed.
Moving on to the second case
of the switch
statement we handle what happens when the player removes their finger from the screen. The ACTION_UP
case is just one line of code.
mBat.setMovementState(mBat.STOPPED);
This line of code effectively cancels any movement.
The overall effect of the entire thing is that the player needs to hold either the left or the right of the screen to move left or right. Note the comments allude to a bug/limitation of the code.
The code detects touches and removing it also detects left and right-hand side. It is, however, possible to trick the system by touching both sides at once and then removing just one finger. We will use more advanced code in the fifth game to solve this problem.
3.139.59.53