Reputation: 2521
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
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
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