MAGS94
MAGS94

Reputation: 510

Actor responding to another InputListerner

Say if I had an Actor that is responding to InputListener

How to stop this actor from responding to another InputListener until the previous one is finished

I have the following classes :

class MyActor

public class MyActor extends Image {
    public MyActor() {
        super(new Texture("img.png"));
        setPosition(100, 100);
        debug();
    }
}

class TestScreen

public class TestScreen implements Screen {
    GameMain gameMain;
    Stage stage;
    MyActor myActor;

    public TestScreen(GameMain gameMain) {
        this.gameMain = gameMain;
    }

    @Override
    public void show() {
        stage = new Stage();
        myActor = new MyActor();

        //addlistener to actor
        myActor.addListener(new InputListener() {
            @Override
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                RotateByAction rba = new RotateByAction();
                rba.setAmount(90.0f);
                rba.setDuration(3.0f);
                myActor.addAction(rba);
                System.out.println("actor is rotating");
                return true;
            }
        });
        stage.addActor(myActor);
        Gdx.input.setInputProcessor(stage);
    }

    @Override
    public void render(float delta) {
        stage.act(delta);
        stage.draw();
    }
}

Upvotes: 0

Views: 39

Answers (1)

Peter R
Peter R

Reputation: 1034

Actor has a getActions() method that lists all current actions. Once an action has finished, it is removed from the Actor. Before allowing your actor to respond to other listener events, check the actor's current actions and make your determination from there. (ie, if your actor still has the previous action then "ignore" the listener's events)

You can also create sequence actions, which are 2 or more actions to be executed consecutively. The 2nd (or last) action could be a RunnableAction and in it's run() method you give further instructions to your Actor (eg: "OK to accept new events"). This second option is kind of like a callback when the other actions have finished.

Upvotes: 1

Related Questions