Java – Android screen joystick issue

Android screen joystick issue… here is a solution to the problem.

Android screen joystick issue

So I’m trying to build a game with an on-screen joystick that moves the bitmap around the screen. But when I hold down the joystick in any direction and hold still, the bitmap also stops moving. The bitmap only moves when I move the joystick. Basically, I want to be able to hold down the joystick in the left position and let the bitmap move to the left until I let go. Everything else in the code is valid. 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;
        }
}

Solution

Try this, which is an open-source API for screen joysticks for Android.

Related Problems and Solutions