How is it possible to pause an action of an actor

2019-08-28 03:20发布

I added the following action to my actor:

this.addAction(sequence(delay(0.5f), alpha(1, 2), delay(2), alpha(0, 2)));

Is there an easy way to pause this animation and then continue it when a button is clicked?

2条回答
小情绪 Triste *
2楼-- · 2019-08-28 03:49

If your actor is only running action, I suggest to stop calling the act() method of the actor. Extend Actor to set a switch if needed.

public void act(){
  if(mUpdateAnimation){
     this.act(delta)
  }
}
查看更多
Emotional °昔
3楼-- · 2019-08-28 03:54

Although the above answer is accepted, but when I tried the above solution, the actor moved faster than before. Instead what we can do is remove the action when we don't want any action to be performed by the actor, and add it again later on.

Try removing the action from the actor on click directly from Actor#actions array, and then add it back on another click. Example:

Here is a sample

final Actor actor = // ... initialize your actor;
final Action action = Actions.forever(Actions.rotateBy(360f, 3f, Interpolation.bounceOut));
actor.addAction(action);

actor.addListener(new ClickListener() {
    @Override
    public void clicked(InputEvent event, float x, float y) {
        Array<Action> actions = actor.getActions();
        if (actions.contains(action, true)) {
            // removing an action with Actor#removeAction(Action) method will reset the action,
            // we don't want that, so we delete it directly from the actions array
            actions.removeValue(action, true);
        } else {
            actor.addAction(action);
        }

    }
});

Thanks @Arctic45 for this approach. more can be found on this stackoverflow link

查看更多
登录 后发表回答