Structure a SpriteKit Scene with Lifecycle and Contact Helpers
Keep scene startup, frame updates, and contact responses understandable.
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 scene accumulates responsibilities quickly
The same scene can load a level, update movement, position the camera, receive physics contacts, and display results. When those responsibilities are mixed into long callbacks, a small change can affect several parts of the game. Desert Ruins uses Scroller2d’s scene hooks to give each responsibility a clear entry point.
Start with the scene base
GameScene subclasses AppSharedSceneBase. During initialization it creates a SceneHooks adapter, assigns customDelegate and contactDelegate, then assembles the level. The adapter forwards lifecycle events to focused methods on GameScene.
Forward events through a weak reference
The excerpt shows the beginning of the sample’s nested SceneHooks class. The scene base retains its delegates, while the adapter’s weak scene reference prevents the adapter from owning the scene. Keep the adapter alive through the delegate properties; avoid introducing a separate strong reference back to the scene.
private final class SceneHooks: @preconcurrency SceneBaseCustomDelegate,
@preconcurrency SceneBaseContactDelegate {
weak var scene: GameScene?
/// Keeps a non-owning link back to the game scene for Scroller2d lifecycle callbacks.
init(_ scene: GameScene) { self.scene = scene }
/// Forwards Scroller2d's scene-presentation callback to game startup.
func didMoveScene(to view: SKView) { scene?.startScene() }
/// Forwards Scroller2d's per-frame callback to gameplay simulation.
func updateScene(_ time: TimeInterval) { scene?.step(time) }
/// Forwards Scroller2d's post-physics callback to camera and bounds cleanup.
func didFinishSceneUpdate() { scene?.finishFrame() }
/// Forwards contacts reported by Scroller2d's scene base to gameplay handling.
func didBeginContact(_ contact: SKPhysicsContact) { scene?.handleContact(contact) }
Choose the right phase for the work
startScene sets gravity and starts background sound when the scene is presented. step handles movement, attacks, and pending respawns during update. finishFrame applies bounds and camera placement after physics. handleContact translates category contacts into checkpoints, pickups, hazards, and finishing. That order makes it easier to trace a collision through the next update.
Verify a contact before adding a rule
installBody defines category, collision, and contact-test masks. If a pickup never triggers its handler, inspect those masks and the actual contacted node. Add one coin or checkpoint rule at a time, then exercise it in the running sample before combining it with enemy damage or replay.
To add a first coin milestone, find the Body.coin branch in Desert Ruins’ handleContact(_:). After coins += 1 and updateCounter(), add this check. Collect three coins, then replay and confirm the message appears at the same point on the next run.
if coins == 3 {
show("THREE COINS COLLECTED")
}
Where Scroller2d saves work
A shared scene base wires frame and contact forwarding in one place. Games can put their own startup, movement, post-action, and contact rules behind the delegate. That makes timing consistent when a second level uses the same mechanics and reduces the chance that a camera update or contact handler is wired at a different phase.