Game Development Reference
In-Depth Information
The onTouchEvent() function is the main hook into where the player's view and player's input are
updated.
The onTouchEvent() function (see Listing 7-13) does the following:
Gets the current x and y screen position and stores it into variables x and y
1.
2.
Based on the user's action, one of the following occurs:
If the user starts a new touch, then this x, y starting location is saved in
m_Startx and
m_Starty .
If the user exits a touch by lifting his/her finger, then the
ProcessTouch() function in
the MyGLRenderer class is called.
If the user moves his/her finger across the screen, the
CameraMoved() function in the
MyGLRenderer class is called to update the player's view.
The current x, y screen location is saved as the previous location in anticipation of the
next onTouchEvent() call.
Listing 7-13. Adding and Modifying the onTouchEvent() Function
@Override
public boolean onTouchEvent(MotionEvent e)
{
// MotionEvent reports input details from the touch screen
// and other input controls. In this case, you are only
// interested in events where the touch position changed.
float x = e.getX();
float y = e.getY();
switch (e.getAction())
{
case MotionEvent.ACTION_DOWN:
m_Startx = x;
m_Starty = y;
break;
case MotionEvent.ACTION_UP:
CustomGLRenderer.ProcessTouch(m_Startx, m_Starty, x, y);
break;
case MotionEvent.ACTION_MOVE:
m_dx = x - m_PreviousX;
m_dy = y - m_PreviousY;
CustomGLRenderer.CameraMoved(m_dy, m_dx);
break;
}
m_PreviousX = x;
m_PreviousY = y;
return true;
}
 
Search WWH ::




Custom Search