Libgdx sprite fades out

I am working on 2D shooting in LibGdx. I should mention that I am new to LibGdx and I am trying very hard to understand how this works. I have had programming experience in Java and Android for several years, so I understand the concepts of the game.

I am wondering if there is a way to disappear from the sprite object.

I have enemies on the screen, and when the enemy is dead, I want to remove the Enemy object from my list and ignore it inside the calculations and intersection logic.

But I want the enemy sprite to stay on the screen a little longer and slowly disappear.

Is there a good way in LibGdx to handle this ... or do I need to draw additional "fade out" frames ... and process it inside the animation ...

Is there a built-in function that supports this view?

Tnx a lot! I need someone to clarify this for me before starting a brainstorming session and losing my life when drawing sprites.

+7
source share
2 answers

You must be able to extinguish your dead enemy sprites, reducing their "alpha" over time. I think the easiest way to do this is to use the setColor() package:

 batch.setColor(1.0f, 1.0f, 1.0f, fadeTimeAlpha); batch.draw(deadEnemySprite, ...); 

You will need to compute fadeTimeAlpha (taking it from 1.0f to 0.0f over time).
Color.lerp() methods can help.

I'm not sure that adjusting the color for each sprite will cause a batch to crash (I suspect this will happen), so this can have relatively high performance (assuming your batch sprite drawing behaved well in advance).

+11
source

Using Sprite, you can use .setAlpha:

 alpha += (1f / 60f) / 2; icon.setAlpha(alpha); 

First of all, the alpha system: you specify a number from 0.0 to 1.0 (not 0 and 255)

in the code snippet above, alpha is a floating-point value, and icon is a sprite. In this case, Alpha is (initially) 0.

to calculate:

 (1f / 60f) / 2; 

1f - max alpha

60f - FPS

and 2 is because I want it to last more than 2 seconds.

(New in LibGDX, so I did not understand how I can get FPS, since this is not necessarily 60).

Changing what you divide by (and changing FPS if necessary) changes how much is added (or removed) from alpha, and how long the animation will take

+1
source

All Articles