如何检测正在使用的手指数量?

How to detect number of fingers being used?

我正在使用 Libgdx 制作游戏,我需要知道用户是否在使用两根手指以及它们的放置位置是否正确。一根手指应在屏幕右侧,另一根手指应在屏幕左侧。

How to detect number of fingers being used?

你可以用 MotionEventgetPointerCount()

您可以检测屏幕上有多少根手指这样做:

int PointerCount = event.getPointerCount();

I need to know if the user is using two fingers and if they are placed in the correct position

您可以获得 X,Y 并进行比较。

@Override
public boolean onTouch(View v, MotionEvent event) {
   int x = event.getX();
   int y = event.getY();
   return true;
}

有关详细信息,您可以查看 MotionEvent Documentation,这样您就可以到达那里。

GestureListenerInputProcessor都有touchDown方法。这具有屏幕上每个手指(指针)的 x 和 y 值。实施其中之一,您可以覆盖触地得分以满足您的需要。 This 是一个很好的入门教程。希望这有帮助。

最简单的解决方案不使用任何侦听器 只是迭代一些指针并调用简单的 Gdx.input.isTouched() - 你必须设置一些 "maximum pointers count"但是嘿-人们通常只有 20 个手指 :)

    final int MAX_NUMBER_OF_POINTERS = 20;
    int pointers = 0;

    for(int i = 0; i < MAX_NUMBER_OF_POINTERS; i++)
    {  
        if( Gdx.input.isTouched(i) ) pointers++;
    }

    System.out.println( pointers );

由于参考:

Whether the screen is currently touched by the pointer with the given index. Pointers are indexed from 0 to n. The pointer id identifies the order in which the fingers went down on the screen, e.g. 0 is the first finger, 1 is the second and so on. When two fingers are touched down and the first one is lifted the second one keeps its index. If another finger is placed on the touch screen the first free index will be used.

您还可以使用Gdx.input.getX()Gdx.input.getY()轻松定位指针的位置,例如

    final int MAX_NUMBER_OF_POINTERS = 20;
    int pointers = 0;

    for(int i = 0; i < MAX_NUMBER_OF_POINTERS; i++)
    {  
        if( Gdx.input.isTouched(i) )
        {
            x = Gdx.input.getX(i);
            y = Gdx.input.getY(i)
        }
    }

然后你可以把它放入数组