nano
nano

Reputation: 2521

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!

Upvotes: 3

Views: 3121

Answers (2)

nano
nano

Reputation: 2521

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

Upvotes: 11

Matthew Tory
Matthew Tory

Reputation: 1306

In order to make the alpha values of your image to work, you need to change the colour of your spriteBatch like so:

Color color = batch.getColor();
batch.setColor(color.r, color.g, color.b, parentAlpha); //Sets the alpha of the batch without changing the color

batch.draw(...);

Upvotes: 1

Related Questions