I'm working on 2D shooting game in LibGdx. I have to mention that I'm new to LibGdx, and I'm trying really hard to understand how it works. I have experience in Java and Android programming for few years, so I understand game concepts.
I'm interested is there a way to fade out sprite object.
I have enemies on screen, and when enemy is dead I want to remove Enemy object from my list and ignore it inside calculations and intersection logic.
But I want to enemy's sprite stay on the screen for a bit longer and to fade out slowly.
Is there a nice way in LibGdx to handle this...or I have to draw some extra "fade out" frames...and to handle it inside animation...
Is there a built in feature that supports this kind of stuff?
Tnx a lot! I need someone to clear that up for me, before I begin to brain storm, and lose lifetime in drawing sprites.
You should be able to fade out your dead enemy sprites by decreasing their "alpha" over time. I think the easiest way to do that is to use batch setColor()
:
batch.setColor(1.0f, 1.0f, 1.0f, fadeTimeAlpha);
batch.draw(deadEnemySprite, ...);
You'll have to compute the fadeTimeAlpha
(taking it from 1.0f to 0.0f over time).
The Color.lerp()
methods might help.
I'm not sure if setting the color for each sprite will cause the batch to flush (I suspect it will), so this might have a relatively high performance cost (assuming your batch sprite drawing was behaving well beforehand).
Using Sprite you can use .setAlpha:
alpha += (1f / 60f) / 2;
icon.setAlpha(alpha);
First off, the alpha system: You supply a number between 0.0 and 1.0 (not 0 and 255)
in the code snippet above, alpha
is a float value and icon
is a Sprite. Alpha is (initially) 0 in this case.
for the calculation:
(1f / 60f) / 2;
1f is max alpha
60f is FPS
and 2 is because I want this to go over 2 seconds.
(New to LibGDX, so I haven't figured out how I can get the FPS, as it isn't necessarily 60).
Changing what you divide by (and changing FPS if relevant) changes how much is added (or removed) from alpha, and how long time the animation will go over
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With