Invincibility Frames
Invincibility frames are a short stretch of protection after a hit lands. While it lasts, further hits change nothing, so one touch cannot drain the character in a blink. Measure the protection in seconds, not screen frames, never let it restart on ignored hits, and let pits and crushing traps ignore it.

Paste this to your AI
Copy it into Claude, Cursor or ChatGPT. It already has good values and the usual mistakes in it.
Add "Invincibility Frames" to my game.
What it should do: Protection after a hit gives the player time to get away, and it stops a character standing in a hazard from losing all its health in a fraction of a second. Done wrong it is either too short to matter or so long that danger stops feeling real.
Build it exactly as this state machine. First check that it fits my game, then build it like this:
```mermaid
stateDiagram-v2
[*] --> vulnerable
vulnerable: Open to hits
protected: Protected
down: Down
vulnerable --> vulnerable: no hit
vulnerable --> protected: takes a hit
vulnerable --> down: hit takes the last health
protected --> protected: hits are ignored
protected --> vulnerable: protection runs out
protected --> down: piercing hit finishes
down --> down: stays down
down --> [*]
```
Use these starting values:
- maxHealth: 6
- protectTime: 1 s
- flickerRate: 10 1/s
Watch out for these common mistakes:
- Counting screen frames instead of seconds: Tell your AI: keep the protection in seconds and take the time since the last tick off it.
- An ignored hit restarts the window: Tell your AI: a hit that is ignored changes nothing at all, so the window ends on time and the next hit lands on time.
- Pits and crushing traps: Tell your AI: give these hazards a piercing hit that ignores protection and leaves the window as it was.
- The flicker decides the hit: Tell your AI: decide hits from the time left only, and draw the flicker from the time left as a separate step.
- A hit in the moment the window ends: Tell your AI: let time pass first, then decide on the hit.
- Overkill and healing from the ground: Tell your AI: report only the health actually taken off, and ignore healing once the character is down.
Do not change anything else in the game. Do not claim anything without proof: model what it takes, and show me what your solution rests on and how I can see that it works. If you cannot show something, say you do not know. When you are done, tell me in plain words how to try it.Or skip this and connect the MCP: your AI then finds all of this by itself.Connect the MCP
What it does
Protection after a hit gives the player time to get away, and it stops a character standing in a hazard from losing all its health in a fraction of a second. Done wrong it is either too short to matter or so long that danger stops feeling real.
How it runs, as a diagram
Everything it can be in, and what moves it on. Your AI builds from exactly this.
How it runs, as a diagram
- Open to hits → Open to hits: no hit
- Open to hits → Protected: takes a hit
- Open to hits → Down: hit takes the last health
- Protected → Protected: hits are ignored
- Protected → Open to hits: protection runs out
- Protected → Down: piercing hit finishes
- Down → Down: stays down
Good starting values
| Value | Start with | What it changes |
|---|---|---|
| maxHealth | 6 | The most health the character can have. Six is a small number on purpose: with one point per ordinary hit and a protection window, the player can see every hit coming and count what is left. |
| protectTime | 1 s | How long a landed hit protects. One second is enough to walk out of an enemy's reach, and short enough that a character who stays in danger is hit again. Under a quarter second players cannot react, over three they ignore enemies. |
| flickerRate | 10 1/s | How often the picture switches between shown and hidden while protected. Ten times a second reads clearly as hurt without hiding where the character is. It changes the picture only, never who can be hit. |
What usually goes wrong
Counting screen frames instead of seconds
A protection of 60 frames lasts one second on one screen and under half a second on a fast one. The game gets harder for people with better hardware.
Fix: Tell your AI: keep the protection in seconds and take the time since the last tick off it.
An ignored hit restarts the window
A character standing in a hazard that touches it every tick is hit once, then protected for as long as it stands there, or loses health the moment protection ends and never gets a chance to leave.
Fix: Tell your AI: a hit that is ignored changes nothing at all, so the window ends on time and the next hit lands on time.
Pits and crushing traps
A pit or a crushing wall that respects protection cannot kill a character who was just hit, so the player drops into a hole and survives it.
Fix: Tell your AI: give these hazards a piercing hit that ignores protection and leaves the window as it was.
The flicker decides the hit
If the game checks whether the picture is shown to decide whether a hit lands, the character can be hit during every other short moment of protection.
Fix: Tell your AI: decide hits from the time left only, and draw the flicker from the time left as a separate step.
A hit in the moment the window ends
If the hit is checked before the time is taken off, a hit in the last tick of protection is ignored, and the window seems a tick longer than set.
Fix: Tell your AI: let time pass first, then decide on the hit.
Overkill and healing from the ground
A hit of 900 on a character with 2 health reports 900 damage, and a heal that arrives after the last hit brings the character back up although it was already down.
Fix: Tell your AI: report only the health actually taken off, and ignore healing once the character is down.
How to check it works
- Your character is open to hits. A hit of 2 lands: The character loses 2 health and a full second of protection starts.
- Your character is protected. Another hit lands: No health is lost and the protection time keeps running down as before.
- Your character was hit one second ago. Another hit lands: This hit lands, because the protection is over.
- A hazard touches your character every tick for four seconds. Watch when it hurts: It hurts once a second, on time, and not less often.
- Your character is protected. A pit or crushing trap hits with a piercing hit: The character loses the health, and the protection left stays as it was.
- Your character is protected and the picture flickers. A hit lands while the picture is shown and another while it is hidden: Neither hit lands.
- A hazard touches your character on every tick for three and a half seconds. Run it at 30, 60, 144 and 240 ticks a second: The character is hurt four times every time.
- Your character has 2 health. A hit of 900 lands, then a heal arrives: Health is zero, only 2 damage is reported and the heal changes nothing.
Works well with
- Health and Damage: Both can share one health number. Keep the protection time in one place. Let health and damage decide how much is lost. Let protection decide whether a hit counts at all.
- Knockback: Protection should last at least as long as the push, so the character is not hit again while flying. Start the push and the protection from the same landed hit.
- Dodge Roll: The two can run side by side. Take the larger time left, never the sum. Give the roll its own protection window beside this one.
Common questions
how long should invincibility frames last after a hit
Start with one second for a normal hit. Under a quarter second players cannot react to it, and over three seconds they stop fearing enemies.
why does my character get hit again right after the protection ends
A hazard that touches every tick lands the moment the window closes. That is correct: the protection is for escaping. Give the player time and a way out, such as a push away from the hazard.
should invincibility frames protect from falling into a pit
No. A pit, a crushing wall or any hazard that is meant to end the run should pierce the protection, or the player survives holes just by having been hit a moment before.
what do i tell my ai to add invincibility frames
Paste this: Add one second of protection after a landed hit, counted in seconds and not in screen frames. Ignored hits change nothing and never restart it. Pits and crushing traps pierce it. The flicker is drawn from the time left and never decides a hit.
Where it comes from
We could not find a reliable origin for this one yet.
What was checked
- 14 tests pass on the reference code.
- The state diagram was checked against the code, at the default values and at every listed low and high value.
Your AI gets the code
Tested example code comes through the Spielerando connection, so your AI can use it right away. How to connect it