2 回答

TA貢獻(xiàn)1786條經(jīng)驗 獲得超11個贊
這是另一個想法。您可以將您的操作包裝在一種新型的可暫停操作中,而不是處理刪除和恢復(fù)操作以及隨后處理池問題。
public class PausableAction extends DelegateAction {
public static PausableAction pausable(Action wrappedAction){
PausableAction action = Actions.action(PausableAction.class);
action.setAction(wrappedAction);
return action;
}
boolean paused = false;
public void pause (){
paused = true;
}
public void unpause (){
paused = false;
}
protected boolean delegate (float delta){
if (paused)
return false;
return action.act(delta);
}
public void restart () {
super.restart();
paused = false;
}
}
現(xiàn)在,在獲取您的操作時,將它們包裝在一個可暫停的對象中,例如:
btn1.addAction(PausableAction.pausable(Actions.scaleBy(1,1,3)));
并在需要時暫停/取消暫停操作,例如:
//...
actor = event.getListenerActor();
actor.setScale(0.9f);
for (Action action : actor.getActions())
if (action instanceof PausableAction)
((PausableAction)action).pause();
return super.touchDown(event, x, y, pointer, button);

TA貢獻(xiàn)1804條經(jīng)驗 獲得超7個贊
來自池(比如來自 Actions 類)的動作的默認(rèn)行為是當(dāng)它們從 actor 中移除時重新啟動它們自己。重用這些實例實際上并不安全,因為它們也已返回到池中并且可能意外地附加到其他一些參與者。
因此,在將它們從 actor 中移除之前,您需要將它們的池設(shè)置為 null。
private static void clearPools (Array<Action> actions){
for (Action action : actions){
action.setPool(null);
if (action instanceof ParallelAction) //SequenceActions are also ParallelActions
clearPools(((ParallelAction)action).getActions());
else if (action instanceof DelegateAction)
((DelegateAction)action).getAction().setPool(null);
}
}
//And right before actor.clearActions();
clearPools(actor.getActions());
然后,當(dāng)您將它們添加回 actor 時,您需要將它們的池添加回來,以便它們可以返回到 Actions 池并在以后重用以避免 GC 攪動。
private static void assignPools (Array<Action> actions){
for (Action action : actions){
action.setPool(Pools.get(action.getClass()));
if (action instanceof ParallelAction)
assignPools(((ParallelAction)action).getActions());
else if (action instanceof DelegateAction){
Action innerAction = ((DelegateAction)action).getAction();
innerAction.setPool(Pools.get(innerAction.getClass()));
}
}
}
//And call it on your actor right after adding the actions back:
assignPools(actor.getActions);
添加回答
舉報