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 am making a simple platform game in Libgdx... in which I have made the player to move left, move right and jump. The code works fine on Desktop but on Android devices, Jump is not fired when the player moves left or right. It looks strange. Here is my code...

private void updatePlayerForUserInput(float deltaTime) {

    // check input and apply to velocity & state
    if ((Gdx.input.isKeyPressed(Keys.SPACE) || isTouched(0.87f, 1,0,1f)) && world.player.grounded)
        world.player.velocity.y += world.player.JUMP_VELOCITY;
        world.player.state =2;
        world.player.grounded = false;

    if (Gdx.input.isKeyPressed(Keys.LEFT) || Gdx.input.isKeyPressed(Keys.A) || isTouched(0, 0.1f,0,1f))
        world.player.velocity.x -=world.player.MAX_VELOCITY;
        if (world.player.grounded)
            world.player.state =1;
        world.player.facesRight = false;

    if (Gdx.input.isKeyPressed(Keys.RIGHT) || Gdx.input.isKeyPressed(Keys.D) || isTouched(0.2f, 0.3f,0,1f))
        world.player.velocity.x =world.player.MAX_VELOCITY;
        if (world.player.grounded)
            world.player.state =1;
        world.player.facesRight = true;


private boolean isTouched(float startX, float endX , float startY, float endY)
    // check if any finge is touch the area between startX and endX
    // startX/endX are given between 0 (left edge of the screen) and 1 (right edge of the screen)
    for (int i = 0; i < 2; i++)
        float x = Gdx.input.getX() / (float);
        float y = Gdx.input.getY() / (float);
        if (Gdx.input.isTouched(i) && (x >= startX && x <= endX) && (y>=startY && y<= endY))
            return true;
    return false;

I took the idea from the demo platform game SuperKoalio by mzencher at

Please suggest

share|improve this question
Kelly is correct. Each time you get the x and y in your isTouched(...) function, they're the same x and y. You're not getting the x and y from the touch event you're processing, you're getting it from the first event (the one telling the player to move left or right). – Byte56 Jun 17 '13 at 13:13

I think you should try Gdx.input.getX(i) and Gdx.input.getY(i)

From the Gdx.input source:

/** @return the last touch x coordinate for the first pointer in screen coordinates. The screen origin is the top left corner. */
public int getX ();

/** Returns the x coordinate in screen coordinates of the given pointer. Pointers are indexed from 0 to n. The pointer id
 * identifies the order in which the fingers went down on the screen, e.g. 0 is the first finger, 1 is the second and so on.
 * When two fingers are touched down and the first one is lifted the second one keeps its index. If another finger is placed on
 * the touch screen the first free index will be used.
 * @param pointer the pointer id.
 * @return the x coordinate */
public int getX (int pointer);


LibGDX supports having multiple pointers when running on a compatible device. As the documentation above states each touch is assigned the lowest available ID but retains it's ID for the duration of the touch. This can get complex when tracking more involved gestures but should be trivial when simulating buttons. I haven't heard of an android device that supports more than 10 fingers at once so I would just set the for loop to query pointer IDs 0 through 9.

It sounds like either your update loop is missing gravity & friction or your input is "sticky" (i.e. behaving like a button that is jammed down). If you add a line just before return true in isTouched(...) that prints the value of startx to the log then you will be able to see if / when it plays up.

share|improve this answer
Hi kelli... I did so, I replaced my code with this code. But.. now If i am pressing Left button, the player continues to move along that...similarly it jumps infintely... What's wrong with the code. Please answer. Is this a better way or I should use Pointers .How to use them. I am confused? ...I've edited my question below..plz see – Vishal Kumar Jun 17 '13 at 14:55

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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