本文介绍了如何从cachedActions Libgdx中继续执行序列/并行操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个clicklistener扩展类,其目的是在touchDown期间缓存角色的任何当前动作,并在触发touchUp时将其分配回去.但是,它不适用于顺序动作或并行动作.

I have a clicklistener extended class which aims to cached any current actions of the actor during touchDown, and assigns it back when touchUp is triggered. However, it does not works for sequence or parallel actions.

public class MyClickListener extends ClickListener {

    public Actor actor;
    private final Array<Action> cachedActions = new Array<Action>();

    @Override
    public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
        super.touchUp(event, x, y, pointer, button);
        actor = event.getListenerActor();
        actor.addAction(btnScaleBackActions());
        for(Action action:cachedActions)
        {
            //action.reset(); // i wants the actor to continue at where it stop
            action.setTarget(actor);
            action.setActor(actor);
            actor.addAction(action);
        }
        cachedActions.clear();
    }

    @Override
    public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
        if(pointer==0)
        {
            actor = event.getListenerActor();
            actor.setScale(0.9f);
            cachedActions.addAll(actor.getActions());
            actor.clearActions();
            return super.touchDown(event, x, y, pointer, button);
        }
        else
        {
            return false;
        }
    }

我的按钮测试:

// button touchUp continue its previous action at where it stop
btn1.addAction(Actions.scaleBy(1,1,3));

// button touchUp not continue it previous actions and complete stop
btn2.addAction(sequence(Actions.scaleBy(1,1,3))); 

// button touchUp give nullException error
btn3.addAction(forever(Actions.scaleBy(1,1,3)));

//error :
Exception in thread "LWJGL Application" java.lang.NullPointerException
        at com.badlogic.gdx.scenes.scene2d.actions.RepeatAction.delegate(RepeatAction.java:29)
        at com.badlogic.gdx.scenes.scene2d.actions.DelegateAction.act(DelegateAction.java:43)

是否可以在myClickListener类停止处继续执行序列/并行操作?

Is it possible to continue sequence/parallel actions at where it stop at myClickListener class?

推荐答案

这是另一种想法.您可以将动作包装在一种新型的可暂停动作中,而不必处理删除和还原动作以及随后处理池问题的情况.

Here's an alternate idea. Rather than deal with removing and restoring actions, and subsequently dealing with the pools issue, you can wrap your actions in a new type of pausable action.

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

现在,在执行操作时,请将其包裹在一个可暂停的位置,例如:

Now when getting your actions, wrap them in a pausable, for example:

btn1.addAction(PausableAction.pausable(Actions.scaleBy(1,1,3)));

并在需要时暂停/取消暂停操作,例如:

And pause/unpause actions when you need to, like:

//...
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);

这篇关于如何从cachedActions Libgdx中继续执行序列/并行操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 23:32