# Fading in and fading out animations

This is how I'm creating my player. Is there any way to start fading in and fading out for an amount of time when I get hit?

private Texture tex = Game.res.getTexture("player");

public Player(Body body) {
TextureRegion[] sprites = new TextureRegion[1];
sprites[0] = new TextureRegion(tex, 0, 0, 70, 96);
sprites[1] = new TextureRegion(tex, 69, 193, 68, 93);
animation.setFrames(sprites, 1 / 6f);
}


## 2 Answers

The solution suggested by Sebastian would work, but you'll end up with a lot of code just for a "fade out/fade in" animation.

Libgdx has built-in functionnalities for that in Scene2D, you should use them. (Actions, Stage, Actor) That would make your code as simple as that :

 myPlayer.addAction(Actions.sequence(Actions.fadeOut(0.15f), Actions.fadeIn(0.15f));


Where myPlayer is an instance of an object extending the Actor class.

Why not set the alpha value of the sprites directly via sprite.setAlpha(float)? Of course you would have to call that by your rendering method and calculate the amount of alpha alteration using deltaTime.

https://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/g2d/Sprite.html#setAlpha-float-