How can I make a LibGDX Actor object flicker over a set duration?

Many 2D platformers have the player character 'flicker' after taking damage to indicate temporary invincibility, (ie: invincibility frames). For an example, see the below gif from Shovel Knight.

Player taking damage from an enemy

It's fairly simple to make an object fade in or out over time using LibGDX's Actions library.

Actor player = new Actor();
// fade out in 0.25 seconds

But searching through the documentation, I couldn't find any method for Actions.flicker(). How can I implement sprite flickering in LibGDX?


Create a flickering SequenceAction and have it repeat a preset number of times.

flicker effect in action

import com.badlogic.gdx.scenes.scene2d.actions.SequenceAction;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;

if (playerDamaged) { 
    SequenceAction flicker = new SequenceAction(Actions.fadeOut(0.25f), Actions.fadeIn(0.25f));
    player.addAction(Actions.repeat(6, flicker));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.