LibGDX - 向上滑动或向右滑动等?
触摸区域http://imageshack.us/a/img836/2909/swipe1.png
在绿色区域,用户可以向上、向右、向下、向左滑动.我现在怎么能得到例如向上滑动?或向下滑动?或向右滑动或向左滑动?例如如何获取字符串 -> input = getSwiped();-> 输入然后向上,或向右,或向下,或向左
In the green area the user can swipe up, right, down, left. How can I now get e.g. swipe Up? or swipe Down? or swipe right or swipe left? e.g. how to get a String -> input = getSwiped(); -> input is then Up, or right, or down, or left
并且用户可以触摸两个按钮.1 或 2.1 是鸭子,2 是跳跃.
And the user can touch two buttons. 1 or 2. 1 is for duck and 2 is for jump.
我想同时检查这些输入.用户可以同时触摸和向上滑动.
I want to check this inputs at the same time. The user can touch and also swipe Up at the same moment.
我知道有一个 GestureDetector.我查看了代码,但不知道如何使用滑动部分.
I know there is a GestureDetector. I looked at the code, but no clue how can I use the swipe part.
我知道一点如何检查按钮.问题只在这里 -> 如何同时检查输入以及如何向上滑动或向右滑动等.
I know a little bit how to check the buttons. The problem is only here -> How to check the inputs at the same time and how get Swipe Up, or Swipe right etc.
我搜索了一个发现如何检查多点触控:
I searched an found how to check Multitouching:
for (int i = 0; i < 10; i++) {
if (Gdx.input.isTouched(i) == false) continue;
float x = Gdx.input.getX(i);
float y = Gdx.graphics.getHeight() - Gdx.input.getY(i) - 1;
//...
}
推荐答案
这解释了一种很好的方法来实现一个系统来检测滑动的方向.我将它发布在这里,因为将来该文章可能会丢失:
This explain a very good way to implement a system to detect the direction of a swipe. I'll post it here because the article may be lost in the future:
创建一个类名 SimpleDirectionGestureDetector
Create a class name SimpleDirectionGestureDetector
public class SimpleDirectionGestureDetector extends GestureDetector {
public interface DirectionListener {
void onLeft();
void onRight();
void onUp();
void onDown();
}
public SimpleDirectionGestureDetector(DirectionListener directionListener) {
super(new DirectionGestureListener(directionListener));
}
private static class DirectionGestureListener extends GestureAdapter{
DirectionListener directionListener;
public DirectionGestureListener(DirectionListener directionListener){
this.directionListener = directionListener;
}
@Override
public boolean fling(float velocityX, float velocityY, int button) {
if(Math.abs(velocityX)>Math.abs(velocityY)){
if(velocityX>0){
directionListener.onRight();
}else{
directionListener.onLeft();
}
}else{
if(velocityY>0){
directionListener.onDown();
}else{
directionListener.onUp();
}
}
return super.fling(velocityX, velocityY, button);
}
}
}
在 LibGdx 应用程序的 create() 函数上,将其用于激活游戏的手势处理:
On the create() function of the LibGdx application, put this to activate gesture handling for your game:
Gdx.input.setInputProcessor(new SimpleDirectionGestureDetector(new SimpleDirectionGestureDetector.DirectionListener() {
@Override
public void onUp() {
}
@Override
public void onRight() {
}
@Override
public void onLeft() {
}
@Override
public void onDown() {
}
}));
相关文章