Reputation: 28
below is my code which I am trying to add touchDragged, I am trying to use the drag method to make an object move left and right by using the touchDragged feature.
Problem is that when i swipe on the right side of the screen be it left or right swipe the object moves right only. when i swipe on the left side be it left or right the object moves left only.
is there something I am missing?
input class and below that the object class.
also is the Tilt action same as this or is that something different? it will be nice if that can be explained.
Input class is as
public class Input implements InputProcessor
{
private Kame myKame;
public Input(Kame kame) {
myKame = kame;
}
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
myKame.onClick();
return true; // Return true to say we handled the touch.
}
@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
myKame.onClick();
return false;
}
@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
myKame.onSwipe(screenX, screenY, pointer);
return true;
}
@Override
public boolean mouseMoved(int screenX, int screenY) {
return false;
}
@Override
public boolean scrolled(int amount) {
return false;
}
}
and Kame class is as :
public class Kame
{
private Vector2 position;
private Vector2 velocity;
private Vector2 acceleration;
private int width;
private int height;
public float x;
public Kame(float x, float y, int width, int height) {
this.width = width;
this.height = height;
position = new Vector2(x, y);
velocity = new Vector2(0, 0);
acceleration = new Vector2(0, 460);
}
public void update(float delta) {
}
public boolean onSwipe(int velocityX, int velocityY, int delta) {
position.add(position.cpy().scl(delta));
// if (Math.abs(velocityX) < Math.abs(velocityY)) {
if (velocityX > 136) {
position.x += 3;//x cordinate
velocity.y += 10;
} else if (velocityX < 136) {
position.x -= 3;
velocity.y += 10;
} else {
// Do nothing.
}
// } else {
// Ignore the input, because we don't care about up/down swipes.
// }
return true;
}
public float getX() {
return position.x;
}
public float getY() {
return position.y;
}
public float getWidth() {
return width;
}
public float getHeight() {
return height;
}
}
Upvotes: 1
Views: 775
Reputation: 88
change the code from
@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
myKame.onSwipe(screenX, screenY, pointer);
return true;
}
@Override
public boolean touchDragged(int screenX, int screenY, int button) {
myKame.onSwipe(screenX, screenY, pointer);
return true;
}
try that
Upvotes: 0
Reputation: 25177
This call to onSwipe
:
@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
myKame.onSwipe(screenX, screenY, pointer);
return true;
}
does not seem to match this implementation of onSwipe
:
public boolean onSwipe(int velocityX, int velocityY, int delta) {
There are a couple problems here.
The screenX
and screenY
parameters passed to touchDragged
are screen coordinates. They range between 0 and the width (or height) of the screen. onSwipe
says it expects a "velocity" (though the algorithm inside doesn't seem to treat it as such).
The pointer
in the touchDragged
signature is the id number of the cursor (think about using 4 fingers on a touchpad, the "pointer" identifies which finger is which). You're using is as a 'delta'.
You can probably just compare the screenX
to pos.x
, and add or subtract 3 from pos.x
depending on if screenX
is less than or greater than pos.x
.
Computing an actual velocity will require tracking the change in location (which means keep track of the previous touch location), and then scaling it by the frame rate (the delta
passed to render
).
Upvotes: 2