Adding Touch Controls

Email In PDF.

This is a small addon-guide to the previousone. We"ll be adding touch controls to the cube. The code is pretty basic but gives an overall idea on how to handle touch.

 

From ModMyGPhone Wiki

We override the onTouchEvent(MotionEvent event)method of our GLSurfaceclass.

switch (event.getAction()) { case MotionEvent.ACTION_DOWN: startX= (int)event.getX(); startY= (int)event.getY(); return true;

Get the X-Y points of the start of touch. For those who have some doubts about touchand tap, a touch has astarting point when finger(or stylus) touches down on the screen ( ACTION_DOWN), amovement when finger slides on the surface ( ACTION_MOVE) and an end pointwhen the finger(or stylus) is lifted up ( ACTION_UP). This completes a single touch. A tap, involves these actions without movement and at a comparitively faster speed.

case MotionEvent.ACTION_MOVE: endY = (int)event.getY(); if((endY-startY)

If endY is less than startY, then the finger moved towards left and cube should rotate to the left.

Similar code for X-axis.

if(resX endY) { mAngle_y += ANGLE_DIFF; } else { mAngle_y += -ANGLE_DIFF; }

This is optional code to prevent wobbling of the cube. If the movement across X-axis was less than the movement across Y-axis, we ignore the X-axis movement.

Value of ANGLE_DIFFis -5.0fwhich is the angle by which the cube rotates.

________________________________________

Paste this method inside the GLSurfaceclass.

public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: startX= (int)event.getX(); startY= (int)event.getY(); return true; case MotionEvent.ACTION_MOVE: endX = (int)event.getX(); endY= (int)event.getY(); if((endY-startY) <0)) { resX= -(endX-startX); } else { resX = (endX-startX); } if(resX endY) { mAngle_y += ANGLE_DIFF; } else { mAngle_y += -ANGLE_DIFF; } } else { if(startX> endX) { mAngle_x += ANGLE_DIFF; } else { mAngle_x += -ANGLE_DIFF; } } return true; } return false; }

Theo gphone

Comments (0)

Write comment

security code
Write the displayed characters


busy

Tin liên quan:
Tin mới hơn:
Tin cũ hơn: