1
\$\begingroup\$

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);
}
\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

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-

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.