Hey all, in this post we’re going over how to add invincibility to your player character in HaxeFlixel. This one is pretty straight-
forward. Let’s get into it.

Why Invincibility

Invincibility frames are an old-school staple. When Marios gets hit in Super Mario Bros. He loses his power up and he starts to flicker indicating to the player that he’s invulnerable. This is important for two reasons. The player needs time to react after getting hit. The player needs a safety net so they don’t die immediately after losing their power up to the same attack.

Creating The Mechanic

We can make use of this in our own game to provide these two benefits to the player. I’ve done it in my own game. Here’s how we set it up on a player character.

import flixel.util.FlxColor;
import flixel.FlxSprite;
class Player extends FlxSprite {
// Whether the player is invicible or not
public var isInvincible:Bool;
//The amount of time to be invincible for in seconds
public static inline var INVINCIBLE_TIME:Float = 1.5;
public function new(x:Float, y:Float) {
super(x, y);
isInvincible = false;
makeGraphic(16, 16, FlxColor.WHITE);
public function startInvincibility() {
isInvincible = true;
//When the time runs out, the player will no longer be invincible.
this.flicker(INVINCIBLE_TIME, 0.04, true, true, (_) -> {
isInvincible = false;
view raw InvincibilityDemo.hx hosted with ❤ by GitHub

See the above. First, we have a property called INVINCIBLE_TIME. This property tells us how long the player should be invincible. We also have another property isInvincible which is used to tell the rest of the game that the player is invincible. The next thing we do is create a function to start the invincibility. We use Flixel’s flicker method to make the sprite flicker. Additionally, that function lets you pass in a callback function and once it’s complete the callback function runs. This brings the character back from being invincible.


With that said, I hope this helps you add another interesting mechanic to your own game. Good luck in game creation and enjoy the process!!

%d bloggers like this: