Search code examples
libgdxactionactoralpha

libgdx actor - alpha actions not being drawn


I have an Image with a fadeOut/fadeIn action. Something like this:

public void fadeInAndOut() {
    AlphaAction actionFadeOut = new AlphaAction();
    actionFadeOut.setAlpha(0f);
    actionFadeOut.setDuration(2f);
    AlphaAction actionFadeIn = new AlphaAction();
    actionFadeIn.setAlpha(1f);
    actionFadeIn.setDuration(2f);

    this.addAction(Actions.sequence(actionFadeOut, Actions.delay(2f), actionFadeIn));
}

But nothing happens when calling this method.

My draw method is:

@Override
public void draw(Batch batch, float parentAlpha) {
    super.draw(batch, parentAlpha);
    batch.draw(objectImage, getX(), getY(), getWidth() * getScaleX(),
            getHeight() * getScaleY());
}

How can I make the alpha values of the image work?

Thanks in advance!


Solution

  • As the scene2d wiki says, we need to override draw like this:

    @Override
    public void draw(Batch batch, float parentAlpha) {
        Color color = getColor();
        batch.setColor(color.r, color.g, color.b, color.a * parentAlpha);
        batch.draw(objectImage, getX(), getY(), getWidth() * getScaleX(),
                getHeight() * getScaleY());
        batch.setColor(color.r, color.g, color.b, 1f);
    }
    

    And voilà...

    ----- UPDATE -----

    I had to add after drawing next line:

    batch.setColor(color.r, color.g, color.b, 1f);
    

    otherwise in some cases the stage color was also affected and not only the actor.

    Hope it helps