Last active
March 17, 2021 22:04
-
-
Save KinoAR/d65a4c0af7d7e264395d01bc136a3be5 to your computer and use it in GitHub Desktop.
Haxe Flixel invincibility demo on a player character.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | |
| }); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Invincibility Example
This demonstrates how to make the user invincible temporarily.
The properties:
Setup the first properties you need for updating the state of the invincibility.
Next, the function startInvincibility handles the rest by starting it whenever you need it in your game and should last for 1 and a half seconds. See below
Here's how you would use it in game when the player gets hit.