Java 如何在Libgdx中检测滑动方向?

Java 如何在Libgdx中检测滑动方向?,java,libgdx,Java,Libgdx,我知道如何在libgdx中检测滑动,但当我滑动到某个方向时,我遇到了一个问题,顺便说一句,我使用了GestureListener if(velocityX > 0) { System.out.println("right"); }else if(velocityY > 0 && velocityX > 0){ System.out.println("down");

我知道如何在libgdx中检测滑动,但当我滑动到某个方向时,我遇到了一个问题,顺便说一句,我使用了GestureListener

        if(velocityX > 0) {
            System.out.println("right");        
        }else if(velocityY > 0 && velocityX > 0){
            System.out.println("down");
        }else if(velocityY < 0){
            System.out.println("up");
        }
if(速度x>0){
System.out.println(“右”);
}否则如果(速度Y>0&&velocityX>0){
System.out.println(“向下”);
}否则如果(速度y<0){
System.out.println(“up”);
}
基本上,当我向上或向下滑动时,它有时会打印出“右”。我想做的是检测指针指向哪个方向,因为如果你使用velocityX和velocityY,那么你沿对角线滑动,它会检测到两个方向,一个是上和右,一个是下和右。在我的例子中,如果用户沿对角线滑动,则只能检测到向上或向下的手势,而不应包括向左和向右滑动

更新:

 if(Math.abs(velocityY) > Math.abs(velocityX)){
          if(velocityY > 0)
              System.out.println("down");
          else if(velocityY < 0)
              System.out.println("up");
      }else if(Math.abs(velocityX) > Math.abs(velocityY))
          if(velocityX > 0)
              System.out.println("right");
if(Math.abs(velocityY)>Math.abs(velocityX)){
如果(速度Y>0)
System.out.println(“向下”);
否则如果(速度y<0)
System.out.println(“up”);
}else if(Math.abs(velocityX)>Math.abs(velocityY))
如果(速度x>0)
System.out.println(“右”);

它基本上打印出大于另一个方向的方向

您可以做的第一件事是确定运动是“更左-右”还是“更上-下”:

然后,选择“更多”方向中的哪一个,例如在条件中的“更左-右”块中:

if (velocityX >= 0) {
  System.out.println("Right");
} else {
  System.out.println("Left");
}
同样适用于
velocityY

请注意,这基本上将方向空间划分为4个象限,在
vy=vx
vy=-vx
行处进行划分


您需要在第一个条件下检查
velocityY
。顺便说一句,目前的情况是,
和&velocityX>0
中总是假的,否则如果
,它会被第一个条件匹配,如果它是真的。@Andy Turner-什么是.abs()方法?因为我尝试了它,它给了我相同的值,例如:velocityY和Math.abs(velocityY)有相同的值。注意:它返回绝对值(x如果x>=0;-x如果x<0)。
if (velocityX >= 0) {
  System.out.println("Right");
} else {
  System.out.println("Left");
}