Omar.KH
Omar.KH

Reputation: 21

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.

Upvotes: 1

Views: 2065

Answers (2)

Gokul Sreenivasan
Gokul Sreenivasan

Reputation: 479

You can implement the long press by implementing the GestureListner interface.

GestureDetector will let you detect the following gestures:

touchDown: A user touches the screen.

longPress: A user touches the screen for some time.

tap: A user touches the screen and lifts the finger again. The finger must not move outside a specified square area around the initial touch position for a tap to be registered. Multiple consecutive taps will be detected if the user performs taps within a specified time interval.

pan: A user drags a finger across the screen. The detector will report the current touch coordinates as well as the delta between the current and previous touch positions. Useful to implement camera panning in 2D. panStop: Called when no longer panning.

fling: A user dragged the finger across the screen, then lifted it. Useful to implement swipe gestures.

zoom: A user places two fingers on the screen and moves them together/apart. The detector will report both the initial and current distance between fingers in pixels. Useful to implement camera zooming.

pinch: Similar to zoom. The detector will report the initial and current finger positions instead of the distance. Useful to implement camera zooming and more sophisticated gestures such as rotation.

A GestureDetector is an event handler. To listen for gestures, one must implement the GestureListener interface and pass it to the constructor of the GestureDetector. The detector is then set as an InputProcessor, either on an InputMultiplexeror as the main InputProcessor:

public class MyGestureListener implements GestureListener{

    @Override
    public boolean touchDown(float x, float y, int pointer, int button) {

        return false;
    }

    @Override
    public boolean tap(float x, float y, int count, int button) {
        return false;
    }

    @Override
    public boolean longPress(float x, float y) {
        return false;
   }

    @Override
    public boolean fling(float velocityX, float velocityY, int button) {
        return false;
    }

    @Override
    public boolean pan(float x, float y, float deltaX, float deltaY) {
        return false;
    }

    @Override
    public boolean panStop(float x, float y, int pointer, int button) {
        return false;
    }

    @Override
    public boolean zoom (float originalDistance, float currentDistance){
       return false;
    }

    @Override
    public boolean pinch (Vector2 initialFirstPointer, Vector2 initialSecondPointer, Vector2 firstPointer, Vector2 secondPointer){
       return false;
    }

    @Override
    public void pinchStop () {
    }
}

You have to set the GestureDetector that contains your GestureListener as the InputProcessor:

     Gdx.input.setInputProcessor(new GestureDetector(new MyGestureListener()));

For more details, check out the link below

https://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/input/GestureDetector.GestureListener.html

Upvotes: 0

Madmenyo
Madmenyo

Reputation: 8584

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.

Upvotes: 1

Related Questions