问题
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