Android屏幕操纵杆问题

时间:2012-07-17 05:20:52

标签: java android multi-touch joystick

因此,我尝试使用屏幕操纵杆构建游戏,该操纵杆可在屏幕上移动位图。但是当我将操纵杆向任何方向向下按住并保持在那里时,位图也将停止移动。它只有在我移动操纵杆时才会移动位图。基本上我希望能够在左侧位置按住操纵杆并让位图向左移动直到我放开。代码中的其他所有内容都有效。有什么建议吗?

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;
        }
}

2 个答案:

答案 0 :(得分:2)

试试this这是适用于Android的屏幕操纵杆的开源API。

答案 1 :(得分:1)

这是因为我只有代码来增加onTouch方法中的位图位置。 当触摸屏幕但没有移动时,未注册事件。下面的代码应该在onTouch方法之外。

bitmapPoint.x += a * .05;
bitmapPoint.y += b * .05;