Search code examples
javaandroidlibgdx

Libgdx - How to use "Long Press"?


I have tried finding an answer to solve the problem, but I think that I don't seem to understand how to use the long press in Libgdx.

I want my character to move right when I long press on the right half of the screen and left when I long press on the left half of the screen.

I have searched and tried.

Here is my InputHandler class :

public class InputHandler implements InputProcessor {
private MainCharacter myMainCharacter;

public InputHandler(MainCharacter mainCharacter) {
    myMainCharacter = mainCharacter;
}

@Override
public boolean keyDown(int keycode) {
    return false;
}

@Override
public boolean keyUp(int keycode) {
    return false;
}

@Override
public boolean keyTyped(char character) {
    return false;
}

@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
    myMainCharacter.onClick();
    return false;
}

@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
    return false;
}

@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
    return false;
}

@Override
public boolean mouseMoved(int screenX, int screenY) {
    return false;
}

@Override
public boolean scrolled(int amount) {
    return false;
}
}

And here is my MainCharacter class :

public class MainCharacter {

private Vector2 position;
private Vector2 velocity;
private Vector2 acceleration;

private float rotation;
private int width;
private int height;

public MainCharacter(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) {
    velocity.add(acceleration.cpy().scl(delta));

    if (velocity.y > 200) {
        velocity.y = 200;
    }

    position.add(velocity.cpy().scl(delta));
}

public void onClick() {
    if (Gdx.input.getX() <= 135) {
        Gdx.app.log("TAG", "LEFT");
        position.x--;
    } else if (Gdx.input.getX() >= 137) {
        Gdx.app.log("TAG", "RIGHT");
        position.x++;
    }
}

public float getX() {
    return position.x;
}

public float getY() {
    return position.y;
}

public float getWidth() {
    return width;
}

public float getHeight() {
    return height;
}

public float getRotation() {
    return rotation;
}
}

I used onClick() method as a replacement until I find a solution for the problem. It works fine but it doesn't have the same effect as the long press. My character moves left when I click on the left side of the screen and right when I click on the right side of the screen. But of course it doesn't work when I long press.

So how can I use 'Long Press' ?

I would really appreciate your help.


Solution

  • Gokul gives a nice overview of the GestureListener but I do not believe this is what you are looking for. LongPress indeed only registers after some seconds of pressing and you want to have a touch control the character immediately.

    There is no out of the box method in the listeners to keep detecting touched but you can create it yourself.

    if (Gdx.input.isTouched())
    {
      //Finger touching the screen
      // You can actually start calling onClick here, if those variables and logic you are using there are correct.
      if (Gdx.input.getX() >= screenSize / 2)
      {
        //Right touched
      }
      else if (Gdx.input.getX() < screenSize / 2)
      {
        //Left touched
      }
    }
    

    Just check for this every frame and do your logic.