【问题标题】:Android onscreen joystick issuesAndroid 屏幕操纵杆问题
【发布时间】:2012-07-17 05:20:52
【问题描述】:

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

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

【问题讨论】:

标签: java android multi-touch joystick


【解决方案1】:

试试this它是Android屏幕操纵杆的开源api。

【讨论】:

  • 这个库工作得很好,需要一些耐心。不要下载 Jar 文件,它在 SVN 源中已经过时了。按照指南并使用“高级方法”,操纵杆将无错误地工作。左/右标签是错误的,但很容易调整。此外,请确保在布局 XML 中设置宽度和高度。此外,将其用于 XML 元素:
【解决方案2】:

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

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

【讨论】:

  • 这是移动你的位图,所以看起来像: _touchingPoint.x = (int) a + initx; _touchingPoint.y = (int) b + inity;
猜你喜欢
  • 1970-01-01
  • 2018-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多