All articles

Add Level Completion Results and Replay to a SpriteKit Game

Connect a finish trigger to results data and a clean new run.

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.

Finishing a level is a state transition

A results panel needs the final coin count and time, and the game needs to stop accepting normal movement. Replay must reset the finished run. Defining those transitions explicitly helps avoid a moving player behind a results screen or collectibles missing from the next attempt.

Configure the Level End presentation

In LevelEditor, inspect the level’s Level End properties. Desert Ruins reads levelEnd.windowItems to build its panel, title, description, counters, and replay item. Position, offset, display size, text, and finish sound come from that configuration.

Choose a gameplay trigger

To require coins before the relic can finish the level, open the Desert Ruins demo’s GameScene.swift and find case Body.relic in handleContact(_:). Place the guard before the existing code that removes the relic.

guard coins >= coinTotal else {
    show("COLLECT ALL COINS FIRST")
    return
}

Touch the relic early to confirm it remains in the scene. Finish collecting the coins, then leave and re-enter the relic’s contact area to trigger the results.

In this sample, touching the relic removes it, increments the relic count, and calls finishLevel(). That method marks the run won, releases controls, and stops horizontal movement. Your own game can call the equivalent transition after a boss encounter or another finish condition.

Fill in the runtime results

The excerpt is part of finishLevel(). It reads the configured result layout and calculates elapsed play time. Later, a switch over windowItemType creates labels and substitutes the actual coin, time, and artifact totals. The saved layout defines where results appear; the game supplies their current values.

guard let config = data.level.levelEnd,
              let items = config.windowItems, !items.isEmpty else {
            show("RELIC RECOVERED  •  TAP RESET TO PLAY AGAIN", persistent: true)
            return
        }

        let shade = SKSpriteNode(color: SKColor(white: 0.04, alpha: 0.72), size: size)
        ending.zPosition = max(hud.zPosition + 1, config.zPosition ?? 600)
        ending.addChild(shade)

        let card = SKNode()
        card.position = config.windowOffset?.toCGPoint() ?? .zero
        card.zPosition = 1
        ending.addChild(card)

        let elapsed = Int(max(0, clock - (runStarted ?? clock)))
        let time = String(format: "%d:%02d", elapsed / 60, elapsed % 60)

Rebuild the run on replay

The sample’s replay path rebuilds terrain, characters, and pickups and clears checkpoint and result state. Test reaching the relic, opening results, replaying, and collecting the same coin again. Also confirm that controls resume and background nodes are rebuilt without accumulating duplicates.

Run and edit Desert Ruins

Where Scroller2d saves work

A reusable results view separates the layout and copy from the values calculated during play. With the saved Level End definition, another level can change the panel, labels, or replay placement while sharing the same runtime results code. Desert Ruins supplies the finish trigger, the totals, and the reset rules.