Frankey
Frankey

Reputation: 336

How to temporary take out actions from actor and assign it back

I am designing my own clickListener class. When i touch down on any actor registered with my clicklistener, i would like to pause all the actions on the actor, and only call it back when touch up is trigger. I tried with the following codes but it give me a total hang everytime i triggered 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)
        );
    }
}

It shows no error but only white screen. Any help?

Upvotes: 1

Views: 43

Answers (1)

Tenfour04
Tenfour04

Reputation: 93779

The problem is this line:

cachedActions = actor.getActions();

You are getting a reference to the Actor's own list of actions instead of making a copy. Incidentally, on the next line (actor.clearActions();) you are clearing the list so cachedActions is empty.

Later on touch up, the actor (and cachedActions) now have the action you added (btnScaleBackActions()). You are cycling through an array, adding the same object to it forever. The iterator can never finish because you are always adding more, so it is an infinite loop.

You need to create your own list for cached actions and copy the items over.

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

Then copy the actions, not the reference in touch down:

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

And make sure to clear cachedActions at the end of touchUp.

Upvotes: 2

Related Questions