0
\$\begingroup\$

Sorry if this is a newbie question, I'm still learning libgdx. I'm using scene2d and I am trying to fade a tooltip into my scene and fade it out after 4.5 seconds.

Everything is working in my code except that tooltip.setVisible() always overrides the fading animation. I'm wondering if there's a way to set up a callback function or if there's any way to tell when an action has completed so I can trigger setVisible after the fade animation has completed. I thought about setting up a second timer that triggers after 5 seconds, but that seems like a hacky way to do things.

Here's my TooltipListener class:

public class TooltipListener extends InputListener {

private Actor tooltip;
private InputListener inputListener;
private Timer timer = new Timer();

public TooltipListener(Actor tooltip) {
    this.tooltip = tooltip;
}

@Override
public boolean touchDown(
    InputEvent event, 
    float x, 
    float y, 
    int pointer, 
    int button
){
    tooltip.setVisible(true);
    tooltip.addAction(fadeIn(1f));
    tooltip.toFront();

    timer.scheduleTask(new Timer.Task() {
        @Override
        public void run() {
            tooltip.addAction(fadeOut(1f));
            tooltip.setVisible(false);
        }
     }, 4.5f);

    return true;
}

}
\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

A nice way to implement what you are trying to achieve would be to use a SequenceAction and built in libgdx actions :

tooltip.addAction(Actions.sequence(
    Actions.show(),
    Actions.fadeIn(1f),
    Actions.delay(4.5f),
    Actions.fadeOut(1f),
    Actions.hide())
)

Actions.show() basically call setVisible(true) on your actor and Action.hide() call setVisible(false).

Be aware that you can also implement your own custom actions, by subclassing the libgdx Action class. For example, to call toFront as you do in your code in an action :

public class ToFrontAction extends Action {

    public boolean act (float delta) {
        target.toFront();
        return true; // An action returns true when it's completed 
    }

} 

So that you will be able to do :

tooltip.addAction(Actions.sequence(
    Actions.show(),
    new ToFrontAction(),
    Actions.fadeIn(1f),
    Actions.delay(4.5f),
    Actions.fadeOut(1f),
    Actions.hide())
)
\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .