Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

libgdx sprite fade out

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.

like image 841
Veljko Avatar asked Mar 10 '13 20:03

Veljko


2 Answers

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).

like image 176
P.T. Avatar answered Nov 09 '22 09:11

P.T.


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

like image 26
Zoe stands with Ukraine Avatar answered Nov 09 '22 10:11

Zoe stands with Ukraine