This is my own coding and it will have some bugs or error but until now its still running well on my apps. Firstly, create a class for handling device input and name it as inputhandler.
public class InputHandler implements InputProcessor {
private int downX;
private int downY;
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
downX = screenX;
downY = screenY;
return true;
}
@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
gameHandler.onSwipe(screenX, screenY, downX, downY);
return true;
}
}
This is just a part of my coding, you should add all the required unimplemented methods and don’t forget to add the necessary Import function or just press Ctrl + o
In your game handler, for example in gameHandler class put this code
public class gameHandler {
public void onSwipe(int X, int Y, int downX, int downY) {
String direction = "";
if(Math.abs(X-downX) > Math.abs(Y-downY)){
if(X > downX){
direction = "right";
} else if(X < downX){
direction = "left";
}
}else{
if(Y > downY){
direction = "down";
} else if(Y < downY){
direction = "up";
}
}
}
}
downX and downY will be the initial position when finger touching the screen while X and Y is the current position when finger touch up the screen. By using this value we can defined the swipe direction.