安卓屏幕手柄问题

4

我正在尝试构建一个带有屏幕摇杆的游戏,可以移动位图在屏幕上移动。但是,当我将摇杆朝任何方向按下并保持不动时,位图也会停止移动。只有当我移动摇杆时,位图才会移动。基本上,我想能够按住摇杆,例如将其放在左侧位置,并使位图向左移动,直到我松开为止。代码中的其他内容都有效。有什么建议吗?

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

http://gamedev.stackexchange.com/questions/17256/examples-of-android-joystick-controls - Ciro Santilli OurBigBook.com
2个回答

2
尝试使用这个吧。它是一个用于Android屏幕摇杆的开源API。

1
这个库在耐心的配合下表现得非常出色。不要下载Jar文件,它已经过时了,来自SVN源代码。按照指南使用"高级方法",摇杆将可以无错误地工作。左/右标签是错误的,但很容易调整。此外,请确保在布局XML中设置宽度和高度。同时,使用以下XML元素:<com.zerokol.views.JoystickView> - Andy

1

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

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

这是移动您的位图,使其看起来像这样:_touchingPoint.x =(int)a + initx; _touchingPoint.y =(int)b + inity; - Andy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接