Part 4 — Enemies, lives and a game-over screen

Danger, a way to lose, and a second scene that lets the player try again.

15 min read

Collecting coins is pleasant but not tense. A game needs a way to lose.

Make an enemy

  1. Press , generator creature, name enemy.
  2. Options: { "palette": "slime", "width": 32, "height": 32 }.
  3. Add it and place one on a platform.
  4. Rename it Enemy and tag it enemy.
  5. Give it dynamic physics so it is affected by gravity.

Make it patrol

  1. With the enemy selected, add the behaviour patrol.
  2. Set distance to 180 and speed to 60.

It now walks back and forth around where you placed it. Slow is usually better than fast: a slow enemy is an obstacle to time, a fast one is a coin flip.

Make it dangerous

  1. Add the behaviour damageOnTouch to the enemy.
  2. Set amount to 1.
  3. Set invulnerableMs to 1000.
  4. Set variable to lives.

Add the lives variable

  1. Open the Assets tab and find Variables.
  2. Add one called lives, type number, value 3.
  3. While you are there add best, type number, value 0, and tick Persist.

A persisted variable is saved to the player's device and restored on load. That is how a high score survives a reload, and it keeps working in an exported game with no server involved.

Add LIVES {lives} to your HUD, either in the same text entity or a second one.

A game-over scene

  1. In the Scenes panel press +.
  2. Name it Game Over and set its Type to menu.
  3. Add a bitmapText entity on the UI layer at 640, 260 with the text GAME OVER and a font size around 64.
  4. Add a second one at 640, 360 with the behaviour bindText and the template SCORE {score} BEST {best}.
  5. Add a third at 640, 480 reading PRESS SPACE TO PLAY AGAIN, and give it the behaviour pulse so it breathes.

Wire the loss

Back on your main scene's graph, add this alongside the coin rule:

On Variable Changed  (lives)
  └─ If   lives <= 0
       true ─→ Record Best   score into best
                 └─ Go To Scene   Game Over
  1. Drag in On Variable Changed and set Variable to lives.
  2. Drag in If and set its expression to lives &lt;= 0.
  3. Wire the event to the If.
  4. Drag in Record Best. Set Current to score and Best to best.
  5. Wire the If's true pin to Record Best.
  6. Drag in Go To Scene, choose Game Over, and set a delay of 600 so the death registers.
  7. Wire Record Best to Go To Scene.

Let them try again

Select the Game Over scene, switch to Logic, and make a graph for it:

On Input  (jump, pressed)
  └─ Set Variable   score = 0
       └─ Set Variable   lives = 3
            └─ Go To Scene   (your main scene)

On Input uses the mapped action jump rather than the Space key directly, which means it also works from the on-screen joystick and survives any rebinding.

Play it

Collect coins, then walk into the enemy three times. You should lose a life per touch — not sixty — see the game-over screen with your score, and be able to restart with Space.