Pause and continue playing an action on actor in Libgdx

♀尐吖头ヾ 提交于 2019-12-24 19:34:07

问题


I am trying out Libgdx. This is what I am trying to achieve,

Define an action on an Actor, while the actor is performing the action, on button click we should be able to start and stop the action, from the same position.

I tried this stackoverflow answer but after starting the action again, the actor moves faster then before, because initially we had set duration for the action to be finished, and since less time is left, it runs faster.

Can you please help? am I missing anything?


回答1:


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

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

    }
});


来源:https://stackoverflow.com/questions/48366997/pause-and-continue-playing-an-action-on-actor-in-libgdx

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