libgdxscene2dbitmap-fonts

BitmapFont animation in Libgdx scene2d Actor


I am trying to animate an actor which contain a simple message written with bitmapfont using fadeIn action but it doesn't work.

Here is my code.

public class Message extends Actor{

        BitmapFont font;
        String message;
        float x, y;

        public Message(BitmapFont font, String message, float x, float y) {
            this.font = font;
            this.message = message;
            this.x = x;
            this.y = y;
        }

        @Override
        public void draw(Batch batch, float parentAlpha) {
            Color color = getColor();
            batch.setColor(color.r, color.g, color.b, color.a * parentAlpha);
            font.setColor(color.r, color.g, color.b, color.a * parentAlpha);
            font.draw(batch, message, x, y);
        }
    }

Then when clicking a button the message should appear but it is only appear without animation.

button.addListener(new ClickListener() {
            @Override
            public void clicked(InputEvent event, float x, float y) {
                BitmapFont font = new BitmapFont(Gdx.files.internal("myfont.font"));
                Message message = new Message(font, "Tap Here", 100, 20);
                stage.addActor(message);
                message.addAction(Actions.fadeIn(5));
            }});

Other actors animations on the same stage work fine as I'm calling:

stage.act(delta);
stage.draw();

Solution

  • You should set message actor's alpha to zero before adding the fadeIn action:

    button.addListener(new ClickListener() {
                @Override
                public void clicked(InputEvent event, float x, float y) {
                    BitmapFont font = new 
                    BitmapFont(Gdx.files.internal("myfont.font"));
                    Message message = new Message(font, "Tap Here", 100, 20);
                    message.getColor().a = 0f; // add this line
                    stage.addActor(message);
                    message.addAction(Actions.fadeIn(5));
                }});