java - Android 屏幕操纵杆问题

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

最佳答案

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

关于java - Android 屏幕操纵杆问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11516431/

相关文章:

java - 如何使用 JWT 集成 Twitter 登录?

windows-7 - 哪些设备可用于在台式机上测试 WM_GESTURE 和 WM_TOUCH 代码?

android - 以编程方式在 Android 中模拟多点触控事件以进行测试

java - 如何在 JavaFx 或 FXML 中将多个 TilePane 放入组/列表中?

java - public static final 需要初始化但 public final 不需要

php - 从 MySql Android 中的多个表中获取数据

java - 当按下 AlertDialog 正按钮时 RecyclerView 是否刷新

android - 审核 APK 时的警告信息

javascript - Internet Explorer 11 是否支持多点触控?

java - 如何为 OkHttp 中的每个请求设置代理?