Search code examples
androidlibgdxtouchgesturelistener

libgdx touchup using gesturelistener


What is the best/easiest way to detect "TouchUp" using the GestureListener in libgdx. (In previous projects I used InputListener and this had TouchUp but no pan or fling).

All I need to do right now is set my MovingLeft or MovingRight booleans to False once the finger is lifted.

package com.moneylife.stashinvaders;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.input.GestureDetector;
import com.badlogic.gdx.math.Vector2;

public class Player {
    Vector2 position;
    Texture texture;
    int speed = 500;
    float deltaTime;
    int screenWidth, screenHeight;
    boolean MovingRight = false, MovingLeft = false;


    public Player(int ScreenW, int ScreenH){
        Gdx.input.setInputProcessor(new GestureDetector(new MyGestureDetector()));
        texture = new Texture("bazookaman.png");
        position = new Vector2(Gdx.graphics.getBackBufferWidth() / 2 - texture.getWidth() / 2, 0);
        screenWidth = ScreenW;
        screenHeight = ScreenH;
    }

    public void update(){
        deltaTime = Gdx.graphics.getDeltaTime();
        if (MovingRight){
            position.x += speed * deltaTime;
        }
        if (MovingLeft){
            position.x -= speed * deltaTime;
        }
    }

    public void draw(SpriteBatch spriteBatch){
        spriteBatch.draw(texture, position.x, position.y);
    }


    public class MyGestureDetector implements GestureDetector.GestureListener {
        @Override
        public boolean touchDown(float x, float y, int pointer, int button) {
            if (x > position.x) {
                MovingRight = true;
            }
            if (x < position.x){
                MovingLeft = true;
            }
            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) {
            MovingLeft = false;
            MovingRight = false;
            return false;
        }

        @Override
        public boolean zoom(float initialDistance, float distance) {
            return false;
        }

        @Override
        public boolean pinch(Vector2 initialPointer1, Vector2 initialPointer2, Vector2 pointer1, Vector2 pointer2) {
            return false;
        }

        @Override
        public void pinchStop() {

        }

    }
}

Solution

  • This was a much simpler way for my basic needs in this project. It's called polling rather than using GestureListener (or inputprocessor) at all:

    if (Gdx.input.isTouched()) {
                if (Gdx.input.getX() > position.x) {
                    position.x += speed * deltaTime;
                }
                if (Gdx.input.getX() < position.x) {
                    position.x -= speed * deltaTime;
                }
            }
    

    Works and player movement is seemless (excluding if the finger goes too near player, but i'm only experimenting here so not worried about that)