我知道如何检测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");
}
Run Code Online (Sandbox Code Playgroud)
基本上发生的是当我向上或向下滑动时,它有时打印出"正确".我想要发生的是检测指针指向哪个方向,因为如果你使用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");
Run Code Online (Sandbox Code Playgroud)
它基本上打印出比另一个方向更大的方向
你要做的第一件事就是决定动作是"更左右"还是"更多上下":
if (Math.abs(velocityX) > Math.abs(velocityY)) {
// More left-right.
} else {
// More up-down.
}
Run Code Online (Sandbox Code Playgroud)
然后,您选择它中的哪个"更多"方向,例如在条件中的"更多左右"块中:
if (velocityX >= 0) {
System.out.println("Right");
} else {
System.out.println("Left");
}
Run Code Online (Sandbox Code Playgroud)
同样的velocityY.
请注意,这基本上将方向空间划分为4个象限,分为线vy = vx和vy = -vx.
| 归档时间: |
|
| 查看次数: |
392 次 |
| 最近记录: |