LibGDX - Get Swipe Up or swipe right etc.?
Asked Answered
P

2

12

touch area http://imageshack.us/a/img836/2909/swipe1.png

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;
    //...
}
Prohibit answered 3/3, 2013 at 12:45 Comment(0)
E
27

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:

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

}
}));
Ellerey answered 10/1, 2014 at 6:52 Comment(5)
I am using this in my game, the problem is whenever I swipe in any direction the game halts for some time (by some time I mean in milliseconds, but it is noticeable). Is there a way to fix it?Mcduffie
is it impossible to know why that happens if you dont show code, that shouldnt happenEllerey
I wonder why it is still not added into Libgdx. BTW, if you want to consume the swipe gesture (for example by InputMultiplexer order) you need to return 'true' from fling methodAitch
@VipulBehl I have the same problem with this basic code example. It lags like 500ms. Did you find a solution? Also I would like to receive a hint of the distance the user swiped across the screen. And if he swiped in the lower up upper half of the screen. Does anyone know how to get this?Unashamed
It seems that fling() is only invoked when the gesture has finished. You could try overriding pan() instead.Disquietude
S
0

i made a simple class to identify the direction of a swipe

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.input.GestureDetector;
import com.badlogic.gdx.math.Vector2;

/**
 * Created by ROHIT on 3/11/2018.
 */

public class swipelistener  {

public boolean isswipright(){
    if(Gdx.input.isTouched()&&Gdx.input.getDeltaX()>0)
        return true;
    return false;
}

public boolean isswipleft(){
    if(Gdx.input.isTouched()&&Gdx.input.getDeltaX()<0)
        return true;
    return false;
}

public boolean isswipup(){
    if(Gdx.input.isTouched()&&Gdx.input.getDeltaY()>0)
        return true;
    return false;
}

public boolean isswipdown(){
    if(Gdx.input.isTouched()&&Gdx.input.getDeltaX()<0)
        return true;
    return false;
}

}

All the above code uses the libGDX Gdx.input.getDeltaX() and Gdx.input.getDeltaY() to get the relative change in direction.

Hope this will help.

Strychninism answered 11/3, 2018 at 8:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.