public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN)
_dragging = true;
else if (event.getAction() == MotionEvent.ACTION_UP)
_dragging = false;
_touchingPoint = new Point();
if (_dragging) {
// get the pos
int x = (int) event.getX();
int y = (int) event.getY();
_touchingPoint.x = x;
_touchingPoint.y = y;
double a = _touchingPoint.x - initx;
double b = _touchingPoint.y - inity;
controllerDistance = Math.sqrt((a * a) + (b * b));
if (controllerDistance > 75) {
a = (a / controllerDistance) * 75;
b = (b / controllerDistance) * 75;
_touchingPoint.x = (int) a + initx;
_touchingPoint.y = (int) b + inity;
}
bitmapPoint.x += a * .05;
bitmapPoint.y += b * .05;
} else if (!_dragging) {
// Snap back to center when the joystick is released
_touchingPoint.x = initx;
_touchingPoint.y = inity;
}
}
#1 楼
试试这个,它是适用于Android屏幕操纵杆的开源api。评论
该库在有些耐心的情况下效果很好。不要下载Jar文件,该文件已从SVN源文件中过期。按照指南进行操作,并使用“高级方法”,操纵杆将正常工作而不会出现错误。左/右标签是错误的,但是很容易调整。另外,请确保在布局XML中设置宽度和高度。另外,将其用于XML元素:
–安迪
2015年3月4日20:26
#2 楼
这是因为我只有onTouch方法中增加位图位置的代码。触摸屏幕但不移动屏幕时,未注册事件。下面的代码应该在onTouch方法之外。
bitmapPoint.x += a * .05;
bitmapPoint.y += b * .05;
评论
这是为了移动您的位图,因此看起来像:_touchingPoint.x =(int)a + initx; _touchingPoint.y =(int)b + inity;
–安迪
2014-12-8 15:42
评论
gamedev.stackexchange.com/questions/17256/…