MAGS94
MAGS94

Reputation: 510

Repeat a Sequence Action in Libgdx

I have an actor called arrow that I want to repeat a sequence action to it.

This arrow points to an actor which if is clicked the arrow should fade out.

Here is my code:

Action moving = Actions.sequence(
                (Actions.moveTo(arrow.getX(), arrow.getY() - 35, 1)),
                (Actions.moveTo(arrow.getX(), arrow.getY(), 1)));
arrow.addAction(moving);
actor.addListener(new ClickListener() {
            @Override
            public void clicked(InputEvent event, float x, float y) {
                arrow.addAction(Actions.fadeOut(1));
            }
        });

Code works fine but I want to repeat 'moving' action untile actor is clicked.

I read about RepeatAction in this question Cannot loop an action. libGDX but I didn't know how I can apply

Upvotes: 1

Views: 943

Answers (1)

Arctic45
Arctic45

Reputation: 1098

You can use RepeatAction in this case, with Actions.forever():

final Action moving = Actions.forever(Actions.sequence(
        (Actions.moveTo(arrow.getX(), arrow.getY() - 35, 1)),
        (Actions.moveTo(arrow.getX(), arrow.getY(), 1))));
arrow.addAction(moving);
actor.addListener(new ClickListener() {
    @Override
    public void clicked(InputEvent event, float x, float y) {
        // you can remove moving action here
        arrow.removeAction(moving);
        arrow.addAction(Actions.fadeOut(1f));
    }
});

If you want to remove arrow from Stage after fading out, you can use RunnableAction:

arrow.addAction(Actions.sequence(
        Actions.fadeOut(1f), Actions.run(new Runnable() {
            @Override
            public void run() {
                arrow.remove();
            }
        }))
);

Upvotes: 3

Related Questions