Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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/game.java I draw my sprite:

@Override
public void create() {
    //...
    batch = new SpriteBatch();
    sheetTexture = new Texture(Gdx.files.internal("animation.png"));
    //...
    InputProcessor ip = new MyInputProcessor();
    Gdx.input.setInputProcessor(ip);
}

Inside game/MyInputProcessor.java

public class MyInputProcessor implements InputProcessor {

    private float lastx;
    private float lasty;

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

In game/game.java 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
    
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 here...like a real anonymous SE bully... –  龚元程 Apr 17 '13 at 18:34
1  
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
add comment

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.

2 Answers

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
add comment

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
add comment

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