libgdx sprite fade out

Veljko picture Veljko · Mar 10, 2013 · Viewed 11.3k times · Source

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.

Answer

P.T. picture P.T. · Mar 11, 2013

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