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

2 に答える 2

2

これを試してくださいこれは、Android用の画面上のジョイスティック用のオープンソースAPIです。

于 2013-06-14T11:02:07.910 に答える
1

これは、onTouchメソッドでビットマップの場所をインクリメントするコードしかなかったためです。画面をタッチしているのに動かしていない場合は登録されません。以下のコードは、onTouchメソッドの外にある必要があります。

bitmapPoint.x += a * .05;
bitmapPoint.y += b * .05;
于 2012-07-20T06:35:27.390 に答える