How is it possible to pause an action of an actor in LibGDX?

╄→尐↘猪︶ㄣ 提交于 2019-12-12 05:03:59

问题


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?


回答1:


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)
  }
}



回答2:


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



来源:https://stackoverflow.com/questions/25511521/how-is-it-possible-to-pause-an-action-of-an-actor-in-libgdx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!