How to intercept touchscreen events in Android OpenGL ES?
How exactly do you intercept touchscreen events for OpenGL ES ga开发者_如何学Gomes in Android? Also, if the game is 3D, how do you know if the user touched an object in the background? Thanks.
Override onTouchEvent(MotionEvent e) into your class extending GlsurfaceView.
@Override public boolean onTouchEvent(MotionEvent e) {
float x = e.getX();
float y = e.getY();
switch (e.getAction()) {
case MotionEvent.ACTION_MOVE:
float dx = x - mPreviousX;
float dy = y - mPreviousY;
mRenderer.mAngleX += dx * TOUCH_SCALE_FACTOR;
mRenderer.mAngleY += dy * TOUCH_SCALE_FACTOR;
requestRender();
}
mPreviousX = x;
mPreviousY = y;
return true;
}
And to know the object touch by user you should compare touch event coordinates with object coordinates.
精彩评论