All articles

Add a Checkpoint and Respawn System in SpriteKit

Connect a checkpoint in level data to safe, predictable respawning.

Follow along with the downloadable Desert Ruins iOS demo. Source references in this article point to Desert Ruins iOS Demo/DesertRuins/GameScene.swift. Code excerpts use the sample’s existing types and properties.

A checkpoint needs more than a flag

A visible beacon tells the player where progress is saved. The game also needs to remember a safe spawn location, detect activation, reset motion after a death, and decide which progress survives. Desert Ruins demonstrates those choices with a beacon before the first gap.

Place the beacon in the level

The checkpoints collection supplies its image, locations, animation start frame, and sounds. GameScene.buildPickups() creates a beacon at the configured position. When gravity placement is enabled, the sample uses determineGroundBaseY to seat it on the assembled terrain. The sample tracks one beacon, so supporting multiple active checkpoints requires extending that runtime state.

Activate it on contact

The contact handler checks that the contacted node is the beacon and that it has not already activated. It records a respawn point using the terrain surface and player body dimensions, disables further beacon contacts, plays its frames, and updates the HUD. The excerpt is part of that handler.

guard !checkpointReached, node === beacon else { return }
            checkpointReached = true
            let foot = determineGroundBaseY(x: node.position.x)
            respawn = CGPoint(x: node.position.x-8, y: foot-playerBodyCenter.y+playerBodySize.height/2+2)
            node.physicsBody = nil

Reset transient movement

On a hazard contact the sample sets pendingRespawn. During the next gameplay update it increments the death count and calls placePlayer(at: respawn). That method clears velocity, buffered jumping, and attack timing. Coins already collected remain collected during this respawn; replay rebuilds the level for a fresh run.

Test the failure path

Activate the beacon, walk into the gap, and confirm that the player returns to stable ground. Repeat while jumping and while slashing. Verify that the character is controllable after each return and that the beacon’s placement leaves enough room for the next attempt.

Run and edit Desert Ruins

Where Scroller2d saves work

The saved checkpoint supplies its placement, image, and animation settings. Move the beacon in LevelEditor and run the same activation and respawn code on the next build. Keep the game’s contact handling and definition of a safe spawn in one place, even when level layouts change.