# Libgdx - gesture dection

I have made a card game, and have two methods that sort the players hand.

sortHandBySuit();
sortHandByValue();


I would like one method to be called when the user slides their finger from left to right across the screen, and the other from right to left.

public class InputHandler implements InputProcessor{
.............
public boolean touchDragged(int screenX, int screenY, int pointer) {
world.getHand().sortHandBySuit();
return false;
}
.............
}


How can I use touchDragged to call my methods?

• Consider editing your post. I think you have some typos that make it very difficult to understand. For instance, I don't think you meant to lcall sortHandBySuit() twice in a row. – Amplify91 Nov 11 '14 at 9:31
• sortHandBySuit(); and sortHandByValue(); I'll be editing that – Asis Nov 11 '14 at 9:38
• Looks much better. I will give an answer now that I can understand the question :) – Amplify91 Nov 12 '14 at 17:40

Not sure if this is the best way for detecting gestures, but this is how I would do it:

On touchDown() I would set two member variables to store where the touch began.

int xStart;
int yStart;

int xDrag;
int yDrag;

boolean gestureStarted = false;

public boolean touchDown(int screenX, int screenY, int pointer) {
xStart = screenX;
yStart = screenY;
return true;
}


Then, in touchDragged(), I would find the last point that the touch is dragged to.

public boolean touchDragged(int screenX, int screenY, int pointer) {
gestureStarted = true;
xDrag = screenX;
yDrag = screenY;
return true;


}

Finally, the gesture will end when they lift their finger from the screen and this is when we compare our values and make our sorting calls.

public boolean touchUp(int screenX, int screenY, int pointer) {
//check that the user hasn't dragged up or down and that we are making a gesture
if(yDrag<yStart+50 && yDrag>yStart-50 && gestureStarted){
//check if user has dragged left
if(xDrag<xStart-50){
sortHandBySuit();
//check if user has dragged right
}else if(xDrag>xStart+50){
sortHandByValue();
}
}
gestureStarted = false;
return true;
}


If you wanted them to be able to swipe back and forth to rapidly change the sorting without lifting their finger, you can do all the value checks and method calls in touchDragged(). This would also make the gestureStarted variable unnecessary. However, without a timer of some sorts, this makes a slow awkward gesture still potentially trigger the sorting and might not be the kind of control you like. You would have to also implement a timer to make sure that the gesture happens within a short period of time (1 second).

public boolean touchDragged(int screenX, int screenY, int pointer) {
//check that the user hasn't dragged up or down and that we are making a gesture
if(yDrag<yStart+50 && yDrag>yStart-50){
//check if user has dragged left
if(xDrag<xStart-50){
sortHandBySuit();
xStart = screenX;
yStart = screenY;
//check if user has dragged right
}else if(xDrag>xStart+50){
sortHandByValue();
xStart = screenX;
yStart = screenY;
}
}
return true;
}

• No problem :) good luck! – Amplify91 Nov 16 '14 at 19:43