Scene2d how to handle a touched actor? (Libgdx)

I have a problem using scene2d in libgdx. I cannot find a method anywhere that allows me to verify that an actor is affected or not. I can find methods that told me if the actor was affected or released. In my game, when an actor is pressed and held, some things must be done in every frame, and not only at one moment when I click on it. I want to stop when I release my finger.

+4
source share
3 answers

You can track it in yours InputListener. Create a boolean field isTouched, set to true when you receive touchDown, false when you receive touchUp. I use this method in my top-down shooter and it works very well.

+4
source

you can check your input just by doing it in the rendering method

gdx.app.log("","touched"+touchdown);

Install the input processor first.

Gdx.input.setInputProcessor(mystage);

then you can add an input listener to your actor in the creation method

optone.addListener(new InputListener() {
        @Override
        public void touchUp(InputEvent event, float x, float y,
                int pointer, int button) {
                boolean touchdown=true;
            //do your stuff 
           //it will work when finger is released..

        }

        public boolean touchDown(InputEvent event, float x, float y,
               int pointer, int button) {
               boolean touchdown=false;
            //do your stuff it will work when u touched your actor
            return true;
        }

    });
+3
source

, , , ; :

:

class MyClickListener extends ClickListener {
    float x, y = 0;

    @Override
    public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
        this.x = x;
        this.y = y;
        return super.touchDown(event, x, y, pointer, button);
    }

    @Override
    public void touchDragged(InputEvent event, float x, float y, int pointer) {
        this.x = x;
        this.y = y;
        super.touchDragged(event, x, y, pointer);
    }
}

:

class MyActor extends Actor {
    private final MyClickListener listener = new MyClickListener();
    MyActor() {
        addListener(listener);
    }
    ...
}

draw (act) :

if (listener.getPressedButton() >= 0)
    System.out.println(listener.x + "; " + listener.y);
0

All Articles