Search code examples
libgdxactionscene2d

Some Actions do not work


I want to make a text appear in the center of the screen, indicating the current level. It should appear gradually and after a while disappear gradually. I'm using scene2d with stages, actors.. so i would use Actions.

This is what i have now:

public class TextActor extends Actor {

    private BitmapFont font;
    private CharSequence charSequence;

    public TextActor(CharSequence charSequence) {
        font = new BitmapFont(Gdx.files.internal("fonts/white_standard_font.fnt"));
        this.charSequence = charSequence;
    }

    @Override
    public void act(float delta) {
        super.act(delta);
    }

    @Override
    public void draw(Batch batch, float delta) {
        super.draw(batch, delta);
        font.draw(batch, charSequence, getX(), getY());
    }
}

In the class that creates the TextActor..

textActor.addAction(Actions.sequence(Actions.fadeIn(1f), Actions.delay(1f), Actions.fadeOut(1f), new Action() {
    @Override
    public boolean act(float delta) {
        textActor.remove();
        transitionInProgress = false;
        gameState = GameState.RUNNING;
        Gdx.input.setInputProcessor(stage);
        return true;
    }
}));
gameTable.addActor(textActor);

fadeIn, fadeOut, alpha.. don't work. I tried with "moveBy" and it works, so it seems a problem concerning the appearance of the Actor. There is something that escapes me.


Solution

  • The fade actions modify the alpha value of the Actor's color (getColor().a). You're drawing the font directly without applying the color associated with the actor.

    Take a look at how Label.draw is implemented for a better understanding. In the meantime, just try adding this above your font.draw(...) call:

    font.setColor(getColor())
    

    Or, if you don't want to modify the entire color, just the alpha, try this:

    font.getColor().a = getColor().a;
    

    UPDATE:

    Also note that you should apply the parentAlpha (second parameter of draw - labelled as delta in your example) to the final alpha:

    font.getColor().a = getColor().a * parentAlpha
    

    This allows your actor to fade if you change the alpha of the stage or any parents.