我在Android上开发一个应用程序ogl
我不使用任何触摸式听众或任何预先制作的东西,因为迟早你会浪费时间想办法做些什么,哈哈。
我发现工作很好的只是
@Override
public boolean onTouchEvent(MotionEvent event) {
if(sr != null){
/*if(event.getAction() == MotionEvent.ACTION_DOWN){
}else if(event.getAction() == MotionEvent.ACTION_MOVE){
}else if(event.getAction() == MotionEvent.ACTION_CANCEL){
}else if(event.getAction() == MotionEvent.ACTION_UP){*/
GameHandler.onTouchEvent(event);
//}
}
return true;
}
在gamehandler中,我首先将触摸屏坐标转换为glworld坐标。
在我的UI元素中,我有位置和大小,并添加了一个方法“touch(…)”或“istouched()”。
根据我的经验,这个很好用,我建议你试试。^^
编辑触摸转换
public static void convertTouchToWorldRef(Vect vect){
vect.y = (SceneRenderer.Height-vect.y)/SceneRenderer.Height*SceneRenderer.GAME_DIMENSION_H;
vect.x = (vect.x)/SceneRenderer.Width*SceneRenderer.GAME_DIMENSION_W;
}
;
你需要在y上做height-y(ogl和android有不同的来源位置)
游戏尺寸是OGL世界投影平面(800*600)的大小。
然后它的基本数学(我记不起英文名),但它的泰利斯公式x1/x1=x2/x2。
编辑:矢量
public class Vector{
private float x = 0;
private float y = 0;
public Vector(float x, float y){
this.x = x;
this.y = y;
}
//add accessors get/set
...
}
你可以浏览互联网寻找更好的例子,比如
http://www.gamedev.net/community/forums/topic.asp?topic_id=302771