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...