Android屏幕操纵杆问题 [英] Android onscreen joystick issues
问题描述
所以我正在尝试使用屏幕上的操纵杆构建一个游戏,该操纵杆可以在屏幕上移动位图。但是当我将操纵杆向任何方向按下并保持在那里时,位图也将停止移动。它只有在我移动操纵杆时才会移动位图。基本上我希望能够在左侧位置按住操纵杆并让位图向左移动直到我放开。代码中的其他所有内容都可以使用。有什么建议吗?
So I'm trying to build a game with an on-screen joystick that moves a bitmap around the screen. But when I hold the joystick down in any direction and keep it there, the bitmap will stop moving as well. Its only when I am moving the joystick does the bitmap move. Basically I want to be able to hold down the joystick in say the left position and have the bitmap move left until I let go. Everything else in the code works. Any suggestions?
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;
}
}
推荐答案
它是因为我只有代码来增加onTouch方法中的位图位置。
当触摸屏幕但未移动时,未注册事件。下面的代码应该在onTouch方法之外。
It was because I only had the code to increment the bitmaps location in the onTouch method. When the screen is being touched but not moving a event is not registered. The code below should be outside the onTouch method.
bitmapPoint.x += a * .05;
bitmapPoint.y += b * .05;
这篇关于Android屏幕操纵杆问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!