Reputation: 33
We are programming a game in Android with libgdx. It's a Doodle Jump like.
At a point of the game, we want plateforms to disapear step by step by reducing the transparency of the picture. We've tested many things but nothing works.
batch.begin();
batch.draw(picture, posX, posY, width, heigth);
bach.end();
We also tried with this, but it doesn't work :
batch.setColor(1, 0, 0, 1);
Upvotes: 3
Views: 225
Reputation: 10320
SpriteBatch#setColor works: you didnt change the alpha (last value), thats why it didnt work for you.
batch.begin();
batch.setColor(1, 1, 1, 0.5F); //0.5F is the alpha value that you want (0-1)
batch.draw(picture, posX, posY, width, heigth);
batch.setColor(1, 1, 1, 1); //change it back to full opacity white, for the other objects to render correctly
bach.end();
If you start using Sprites, mrzli answer is the way to go.
Upvotes: 2
Reputation: 17369
You could use a Sprite, and then set alpha using this method:
public static void setSpriteAlpha(Sprite sprite, float alpha) {
Color c = sprite.getColor();
sprite.setColor(c.r, c.g, c.b, alpha);
}
This works for sure, since I used it in my Doodle Jump clone.
Here is some example usage:
batch.begin();
sprite.setBounds(posX, posY, width, height);
setSpriteAlpha(sprite, 0.5f);
sprite.draw(batch);
batch.end();
If it helps you, you can find the source code for the entire game here, and use it pretty much any way you want to (MIT licence). It is simple and probably not much fun, but it is completed.
You can also check out the result on Google Play.
Upvotes: 1