Blinking text in libGDX after specified event

I would like to make a full screen blinking text, that appear after a specified event, touchDown and after a few blinks disappears. I have found some native android solutions, but it should work with labels, beacuse an integer number should be shown in “blinking mode”.

If it’s a label then it’s an Actor, which has setVisible() or setColor() to make it invisible. You could even tween the fade to zero alpha and back via Actions.

I made this class below. Keep in mind, it calls some of my game-specific functions to determine the “Game clock”, change accordingly.

As it is, it doesn’t blink, it shows for a few seconds, then fades out. Change the getAlpha method for different behaviors, like flashing N times, etc. Easy enough.


public class FadingLabel extends Label{
	
	public static final float DURATION=8f;
	public float creationTime;
	
	public FadingLabel(CharSequence text) {
		super(text, GameAssets.consoleLabelStyle);
		creationTime=GameData.getInstance().gameTime;
	}
	
	private float getAlpha(){			
		float age=GameData.getInstance().gameTime-creationTime;		
		if (age>DURATION) return 0;					
		if (age==0)	return 0;		
		if (age<1) return 1f/age;			
		if (age<DURATION-1) return 1;
			
		return DURATION-age;		
	}
	
	public void activate(String _text){
		creationTime=GameData.getInstance().gameTime;
		setText(_text);
	}
	
	public void draw (Batch batch, float parentAlpha) {		
		setColor(1,1,1,getAlpha());
		super.draw(batch, parentAlpha);
	}

}

Every time you want it to blink, call activate. Otherwise it’ll be invisible.