I want to implement a touch control for an object.When I swipe continuously, without taking the finger from screen,object also has to move along that way.
1.if single touch,object should not move.
2.if swipes/drags a little,object has to move little in that direction.if swipes/drags more,object should move more.
I found this game in play store with the same type of control I want to implement.
https://play.google.com/store/apps/details?id=com.bentostudio.ballsvsblocks&hl=en
I implemented basic swipe control like this.
fling() in my inputprocessor class is like this ;
public boolean fling(float velocityX, float velocityY, int button) {
if (Math.abs(velocityX) > Math.abs(velocityY)) {
if (velocityX > 8) {
this.isSwipeRight = true;
} else if (velocityX < 0) {
this.isSwipeLeft = true;
}
}
return false;
}
and handling swipe in update() like this:
private void handleSwipe() {
if (MyInputProcessor.isSwipeLeft) {
obj.moveLeft();
System.out.println("u swiped left");
MyInputProcessor.isSwipeLeft = false;
} else if (MyInputProcessor.isSwipeRight) {
obj.moveRight();
System.out.println("u swiped right");
MyInputProcessor.isSwipeRight = false;
}
}
and method to move the object:
private float xSpeed=0;
private float leftAccel = 5f;
private float rightAccel = 5f;
public void moveLeft() {
xSpeed-= leftAccel;
setPosition(getX()+xSpeed, getY());
}
public void moveRight() {
xSpeed= rightAccel;
setPosition(getX()+xSpeed, getY());
}
But I am not able to make it work.Only basic swipe is working.Wondering how can I make that smoothness while finger swipe on screen.
Is touchDragged() is the better option for this?
Swing is for quick short moves. For longer continuous finger move you should us pan method: