Making sense of the screen touches

We know that when the player touches the screen the operating system calls our onTouchEvent method to give our code the opportunity to respond to the touch.

Furthermore, we have also seen that when the player touches the screen the onTouchEvent method is called twice. We know this because of the debugging output we examined back in chapter two. You probably remember that the method is called for both the touch and release events.

To make our game respond correctly to touches we will need to first determine the actual event type and secondly find out exactly where on the screen the touch occurred.

Look at the signature of the onTouchEvent method and pay special attention to the highlighted argument.

public boolean onTouchEvent(MotionEvent motionEvent) {

Even though our knowledge of classes and objects is still sketchy, our knowledge of methods should help us work out what is going on here. An object of type MotionEvent named motionEvent is passed into the method.

If we want to know the details of the touch event then it is this object, motionEvent that we will need to unpack.

Remember the Point object that we called size in the onCreate method? Here it is again as a reminder.

// Get the current device's screen resolution
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);

That had two variables contained within it, x and y as shown again highlighted next.

// Initialize our size based variables based 
// on the screen resolution
numberHorizontalPixels = size.x;
numberVerticalPixels = size.y;
blockSize = numberHorizontalPixels / gridWidth;
gridHeight = numberVerticalPixels / blockSize;

It turns out that motionEvent also has a whole bunch of data tucked away inside of it and this data contains the details of the touch that just occurred. The operating system sent it to us because it knows we will probably need some of it.

Notice I said some of it. The MotionEvent class is quite extensive. It contains within it dozens of methods and variables.

Note

Over the course of this book, we will uncover quite a lot of them but nowhere near all of them. You can explore the MotionEvent class here: https://stuff.mit.edu/afs/sipb/project/android/docs/reference/android/view/MotionEvent.html. Note that it is not necessary to do further research to complete this book.

For now, all we need to know is what were the screen coordinates at the precise moment when the player's finger was removed from the screen.

Some of the variables and methods contained within motionEvent that we will use include the following.

  • The getAction method that unsurprisingly "gets" the action that was performed. Unfortunately, it supplies this information in a slightly encoded format which explains the need for some of these other variables.
  • The ACTION_MASK variable which provides a value known as a mask which with the help of a little bit more Java trickery can be used to filter the data from getAction.
  • The ACTION_UP variable that we can use to compare to see if the action performed is the one (removing a finger) we want to respond to.
  • The getX method tells us a horizontal floating-point coordinate where the event happened
  • The getY method tells us a vertical floating-point coordinate where the event happened

So, we need to filter the data returned by getAction using ACTION_MASK and see if the result is the same as ACTION_UP. If it is then we know that the player has just removed their finger from the screen. Once we are sure the event is of the correct type we will need to find out where it happened using getX and getY.

There is one final complication. The Java trickery I referred to is the & bitwise operator. Not to be confused with the logical && operator we have been using in conjunction with the if keyword and loops.

The & bitwise operator checks to see if each corresponding part in two values is true. This is the filter that is required when using ACTION_MASK with getAction.

Note

Sanity check. I was hesitant to go into detail about MotionEvent and bitwise operators. It is possible to complete this entire book and even a professional quality game without ever needing to fully understand them. If you know that the line of code we write in the next section determines the event type the player has just triggered, that is all you need to know. I just guessed that a discerning reader such as yourself would like to know the ins and outs. In summary, if you understand bitwise operators, great you are good to go. If you don't, doesn't matter, you are still good to go. If you are curious about bitwise operators (there are quite a few) you can read more about them here: https://en.wikipedia.org/wiki/Bitwise_operation

Now we can code the onTouchEvent method and see all the MotionEvent stuff in action.