LibGDX - 获取向上滑动或向右滑动等? [英] LibGDX - Get Swipe Up or swipe right etc.?

查看:206
本文介绍了LibGDX - 获取向上滑动或向右滑动等?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在绿色区域,用户可以刷卡上,右,下,左。 我怎么能现在得到如刷卡了?或向下滑动?或向右滑动或刷卡剩下什么? 例如如何得到一个字符串 - >输入= getSwiped(); - >输入,然后向上,或右,或下,或左

和用户可触摸两个按钮。 1或2。 1是鸭和2是跳。

我想在同一时间检查此输入。用户可触摸并滑动向上在同一时刻。

我知道有一个GestureDetector。我看了看code,但不知道我怎么可以用刷卡的部分。

我知道一点点如何检查按钮。问题是,只有在这里 - >如何检查输入的同时,如何让向上滑动,或向右滑动等

我搜索一个发现了如何检查Multitouching:

 的for(int i = 0;我小于10;我++){
    如果(Gdx.input.isTouched(一)==假)继续;
    浮X = Gdx.input.getX(ⅰ);
    浮Y = Gdx.graphics.getHeight() -  Gdx.input.getY(ⅰ) -  1;
    // ...
}
 

解决方案

解释实施一套系统来检测交换的方向迈出的非常好的方法。我将它张贴在这里,因为文章可能会丢失在未来:

创建一个类名SimpleDirectionGestureDetector

 公共类SimpleDirectionGestureDetector扩展GestureDetector {
公共接口DirectionListener {
    无效onLeft();

    无效onRight();

    无效onUp();

    无效onDown();
}

公共SimpleDirectionGestureDetector(DirectionListener directionListener){
    超(新DirectionGestureListener(directionListener));
}

私有静态类DirectionGestureListener扩展GestureAdapter {
    DirectionListener directionListener;

    公共DirectionGestureListener(DirectionListener directionListener){
        this.directionListener = directionListener;
    }

    @覆盖
    公共布尔一扔(浮点velocityX,浮velocityY,INT按钮){
        如果(Math.abs(velocityX)> Math.abs(velocityY)){
            如果(velocityX大于0){
                    directionListener.onRight();
            }其他{
                    directionListener.onLeft();
            }
        }其他{
            如果(velocityY大于0){
                    directionListener.onDown();
            }其他{
                    directionListener.onUp();
            }
        }
        返回super.fling(velocityX,velocityY,按钮);
    }

}

}
 

在LibGdx应用程序的创建()函数,把这个激活手势操作为您的游戏:

  Gdx.input.setInputProcessor(新SimpleDirectionGestureDetector(新SimpleDirectionGestureDetector.DirectionListener(){

@覆盖
公共无效onUp(){

}

@覆盖
公共无效onRight(){

}

@覆盖
公共无效onLeft(){

}

@覆盖
公共无效onDown(){

}
}));
 

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

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.

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 swap. I'll post it here because the article may be lost in the future:

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

}

}

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() {

}
}));

这篇关于LibGDX - 获取向上滑动或向右滑动等?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆