Search code examples
libgdxfadeinactorfadeout

FadeIn Action in LibGDX not working correctly


Please, help me. I try:

actor.addAction(Actions.fadeOut(1.5f));

I get: All my Stage is fading out.

So, my question is why fading out not only one actor, for which I applied fadeOut action, but all my Stage (or at least some other actors)?

My render:

@Override
    public void render(float delta) {
        Gdx.gl.glClearColor(255, 255, 255, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        stage.draw();
        stage.act();
    }

Thank you for your answers.


Solution

  • Any actor class of your own must apply its own color to the batch. This is unnecessary with things like BitmapFont and Sprite, which pass colored vertices directly to the batch, but any actor that calls batch.draw will need to first pass a color, even if it's Color.WHITE, but generally you will want to pass getColor() to the batch.

    This is because there is no guarantee of what color has been left in the batch from any previously drawn actors.

    Also, if your actors are in groups and you want them to fade with their parent, then they should take parent alpha into account:

    Color color = getColor();
    batch.setColor(color.r, color.g, color.b, color.a * parentAlpha);