Making sense of 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 2, Java – First Contact. 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 determine the actual event type and 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 incomplete, our knowledge of methods should help us work out what is going on here. An object of the MotionEvent
...