Game Development Reference
In-Depth Information
The
ProcessTouch()
function processes a user's touch. The starting point (Startx, Starty), at which
the user touches the screen, and the ending point, at which the user lifts his or her finger from the
screen (x,y), are input parameters.
The
ProcessTouch()
function (see Listing 7-12) does the following:
1.
Finds the distance between the point at which the user touches the screen
and the point at which the user lifts his or her finger
2.
Sets the variable that keeps track of screen touches to true and sets the x, y
screen coordinates of the touch location (
m_TouchX
,
m_TouchY
) if this distance
is less than 10 (which means the intention of the user was to touch the
screen to fire a weapon, instead of moving the view)
Listing 7-12. Processing the User's Touch
void ProcessTouch(float Startx, float Starty, float x, float y)
{
Vector3 DiffVec = new Vector3(Startx - x, Starty - y, 0);
float length = DiffVec.Length();
if (length < 10)
{
// Player weapon has been fired
m_ScreenTouched = true;
m_TouchX = x;
m_TouchY = y;
}
}
Modifying the MyGLSurfaceView Class
The MyGLSurfaceView class also has to be modified to provide support for the player's view and
player's touch input.
The
m_PreviousX
and
m_PreviousY
variables track the x and y screen locations of the last user's
touch in the
onTouchEvent()
function.
private float m_PreviousX = 0;
private float m_PreviousY = 0;
The
m_dx
and
m_dy
variables hold the changes in x and y screen positions occurring when the user
touches the screen.
private float m_dx = 0;
private float m_dy = 0;
The
m_Startx
and
m_Starty
variables hold the x and y screen positions when the user first touches
the screen.
private float m_Startx = 0;
private float m_Starty = 0;