Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have 1 simple sprite on the screen and I'd like to move it with the finger. (not necessarily by putting the finger on it, just anywhere on the screen)

I built the project using gdx-setup-ui.jar which created 3 projects (game, game-android, game-desktop)

Inside game/ I draw my sprite:

public void create() {
    batch = new SpriteBatch();
    sheetTexture = new Texture(Gdx.files.internal("animation.png"));
    InputProcessor ip = new MyInputProcessor();

Inside game/

public class MyInputProcessor implements InputProcessor {

    private float lastx;
    private float lasty;

    public boolean touchDragged (int x, int y, int pointer) {
    if(x > lastx)
        game.xPosSprite += game.speed_sprite;
        game.xPosSprite -= game.speed_sprite;
    if(y > lasty)
        game.yPosSprite -= game.speed_sprite;
        game.yPosSprite += game.speed_sprite;
    lastx = x;
    lasty = y;
    return true;

In game/ the variables are declared like this:

public static float xPosSprite;
public static float yPosSprite;
public static float speed_sprite = 0.2f;

This code makes the sprite move very approximately according to the finger movement but it's very sloppy, especially on the x axis.

The lack of very simple code example and the number of methods to get user input makes the exercise difficult.

share|improve this question

closed as too localized by Anko, Josh Petrie, Byte56, msell, ClassicThunder Apr 25 '13 at 15:48

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

haha the downvoter left a comment "So what's the question??" Well, the question is can you read? He realized he couldn't so he deleted the comment. The downvote is still a real anonymous SE bully... – 龚元程 Apr 17 '13 at 18:34
I'll comment with my down vote. This question is too localized for the site. Your title asks how to move a sprite, but the body of the question asks, "How do I fix my code to move a sprite?". I agree with whoever commented that the question is not clear. Anonymously downvoting doesn't imply bully, but attempting to shame someone for asking you to clarify your question does (since you don't even know if it was the same person that down voted). Please keep it professional. Thanks. – Byte56 Apr 18 '13 at 15:10

In your touchDragged function you move the object a set distance. but you only seem to move the sprite based on its initial position, not the position of the touch.

Shouldn't you move the object towards the X and Y sent into touchDragged so that it would end up under your finger?

I would save the XY from touchDragged and send it to the object so that during the objects update it can move itself towards the XY that way you get smooth movement to your finger even if you keep your finger still.

share|improve this answer

Calculate the distance

distance = Math.sqrt(Math.pow(x - lastx,2) + Math.pow(y - lasty, 2));

Create two LigGDX vectors:

Vector2 start = new Vector2(lastx, lasty);
Vector2 end = new Vector2(x,y);

Calculate the delta

Vector2 delta = end.sub(start);
delta.nor(); // Normalize the vector or you get odd diagonal movement

Vector2 position = Vector2(x,y);
position += delta * distance;
x = position.x;
y = position.y;
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.