如何临时从演员那里取出动作并分配回去

时间:2019-05-30 06:26:22

标签: java libgdx

我正在设计自己的clickListener类。当我按下在我的点击侦听器中注册的任何演员时,我想暂停该演员的所有动作,并且仅在触发了修饰时才回叫它。我尝试使用以下代码,但每次触发touchUp都会使我完全挂起。

public class MyClickListener extends ClickListener {

    public Actor actor;
    Array<Action> cachedActions;

    @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 a: cachedActions)
        {
            a.reset();
            a.setTarget(actor); 
            a.setActor(actor);
            actor.addAction(a); //this line give me a total hang
        }
    }

    @Override
    public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
        if(pointer==0) // avoid further trigger on other buttons while holding the selected actor
        {
            actor = event.getListenerActor();
            actor.setScale(0.9f);
            cachedActions = actor.getActions();
            actor.clearActions();
            if(autoSetSound)AudioManager.playSound(AudioManager.CLICK_IN);
            return super.touchDown(event, x, y, pointer, button);
        }
        else
        {
            return false;
        }
    }

    public static Action btnScaleBackActions(){
        float time = 0.1f;
        return sequence(
                scaleTo(1,1,time ),
                scaleTo(0.95f,0.95f,time/4),
                scaleTo(1,1,time/4)
        );
    }
}

它没有显示错误,只有白屏。有帮助吗?

1 个答案:

答案 0 :(得分:2)

问题是这一行:

cachedActions = actor.getActions();

您获得的是Actor自己的动作列表的引用,而不是复制副本。顺便说一下,在下一行(actor.clearActions();)上您正在清除列表,因此cachedActions为空。

修饰后,演员(和cachedActions)现在具有您添加的操作(btnScaleBackActions())。您正在遍历一个数组,将相同的对象永久添加到其中。迭代器永远无法完成,因为您总是在添加更多,所以它是一个无限循环。

您需要为缓存的操作创建自己的列表并将其复制到上面。

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

然后复制操作,而不是接地的引用:

cachedActions.addAll(actor.getActions());
actor.clearActions();

并确保在cachedActions的末尾清除touchUp