8

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.

Veljko
  • 1,893
  • 6
  • 28
  • 58
  • Are you using the internal Stage2D for your sprites? – joey.enfield Mar 11 '13 at 11:34
  • No, I'm using batch to draw my textures and sprites...I use Stage2D for on screen stuff, like pause button, etc. But all sprites and particles are drawn via SpriteBatch. – Veljko Mar 11 '13 at 13:31
  • You'll probally have to write your own then. If you were using Stage2D you could use an Action framework to complete your fade out. You could still do it, just handle the action code yourself. Have a look at AlphaAction. – joey.enfield Mar 11 '13 at 13:39
  • ok, I will take a look, tnx for your answer. I will feedback if I succeed something. – Veljko Mar 11 '13 at 14:27
  • 1
    I found something called TweenEngine...hope I can get this working using that. – Veljko Mar 11 '13 at 14:48
  • Yea, the Action framework is built using the TweenEngine. Its very powerfull, have a look at [the wiki](https://code.google.com/p/libgdx/wiki/scene2d) for some information on the Action framework. – joey.enfield Mar 11 '13 at 16:26
  • [Link update](https://github.com/libgdx/libgdx/wiki/Scene2d) from @joey.enfield's comment – Zoe Jul 16 '17 at 11:53

2 Answers2

13

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

P.T.
  • 24,557
  • 7
  • 64
  • 95
  • Tnx for answer! I will try that out, too. When I change batch color, does it mean that everything that I draw with that batch object will be fading out? May I have multiple batch instances in game screen? – Veljko Mar 11 '13 at 18:04
  • @Veljko it's probably too late, but yes, that means everything will be faded out. You have to reset it with `setColor(1, 1, 1, 1)` between draws. – ashes999 Aug 21 '14 at 03:48
  • Looking at the SpriteBatch code it doesn't look like setting the colour causes a flush. The colour is set in the vertices array when draw is called. – Will Calderwood Dec 02 '14 at 12:45
4

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

Zoe
  • 27,060
  • 21
  • 118
  • 148