SpriteKit
Build 2D games and interactive animations for iOS 26+ using SpriteKit and
Swift 6.3. Covers scene lifecycle, node hierarchy, actions, physics, particles,
camera, touch handling, and SwiftUI integration.
Contents
Workflow
- Establish scene size, scale mode, coordinate system, and ownership before adding gameplay nodes.
- Keep gameplay state in the scene or a stable model; do not recreate the scene during SwiftUI updates.
- Configure node names, z-order, actions, physics categories, and contact masks deliberately.
- Separate camera/HUD coordinates from world coordinates and remove transient nodes predictably.
- Verify frame-rate behavior, contact delivery, pause/resume, resizing, and scene teardown.
Route by Task
- Read core implementation details for scene setup, sprites, actions, physics, touch handling, camera, particles, and SwiftUI integration.
- Read extended SpriteKit patterns for tile maps, atlases, shaders, advanced camera work, audio, and performance recipes.
Core Decisions
- Choose the scene coordinate and scaling contract before positioning content.
- Use category and contact masks as a reviewed collision matrix.
- Keep
SKScene identity stable when hosted by SpriteView.
- Prefer textures and atlases over expensive shape-node rendering in repeated content.
Common Mistakes
Creating a new scene on every SwiftUI re-render
// DON'T: Scene is recreated on every body evaluation
var body: some View {
SpriteView(scene: GameScene(size: CGSize(width: 390, height: 844)))
}
// DO: Create once and reuse
@State private var scene = GameScene(size: CGSize(width: 390, height: 844))
var body: some View {
SpriteView(scene: scene)
}
Adding a child node that already has a parent
A node can only have one parent. Remove from the current parent first or
create a separate instance. Adding a node that already has a parent crashes.
Forgetting to set contactTestBitMask
// DON'T: Bodies collide but didBegin is never called
player.physicsBody?.categoryBitMask = PhysicsCategory.player
enemy.physicsBody?.categoryBitMask = PhysicsCategory.enemy
// DO: Set contactTestBitMask to receive contact callbacks
player.physicsBody?.contactTestBitMask = PhysicsCategory.enemy
Using SKShapeNode for performance-critical rendering
SKShapeNode uses a separate draw call per instance. Prefer SKSpriteNode
with a texture for repeated elements to enable batched rendering.
Not removing nodes that leave the screen
// DON'T
enemy.run(SKAction.moveBy(x: -800, y: 0, duration: 3.0))
addChild(enemy)
// DO: Remove after leaving the visible area
enemy.run(SKAction.sequence([
SKAction.moveBy(x: -800, y: 0, duration: 3.0),
SKAction.removeFromParent()
]))
addChild(enemy)
Setting physicsWorld.contactDelegate too late
Set physicsWorld.contactDelegate = self in didMove(to:), not in
update(_:) or after a delay.
Review Checklist
References
1---2name: spritekit3description: Build 2D games and animations using SpriteKit. Use when creating game scenes with SKScene and SKView, adding sprites with SKSpriteNode, animating with SKAction sequences, simulating physics with SKPhysicsBody and contact detection, creating particle effects with SKEmitterNode, building tile maps, using SKCameraNode, or integrating SpriteKit scenes in SwiftUI with SpriteView.4---56# SpriteKit78Build 2D games and interactive animations for iOS 26+ using SpriteKit and9Swift 6.3. Covers scene lifecycle, node hierarchy, actions, physics, particles,10camera, touch handling, and SwiftUI integration.1112## Contents1314- [Workflow](#workflow)15- [Route by Task](#route-by-task)16- [Core Decisions](#core-decisions)17- [Common Mistakes](#common-mistakes)18- [Review Checklist](#review-checklist)19- [References](#references)2021## Workflow22231. Establish scene size, scale mode, coordinate system, and ownership before adding gameplay nodes.242. Keep gameplay state in the scene or a stable model; do not recreate the scene during SwiftUI updates.253. Configure node names, z-order, actions, physics categories, and contact masks deliberately.264. Separate camera/HUD coordinates from world coordinates and remove transient nodes predictably.275. Verify frame-rate behavior, contact delivery, pause/resume, resizing, and scene teardown.2829## Route by Task3031- Read [core implementation details](references/core-implementation.md) for scene setup, sprites, actions, physics, touch handling, camera, particles, and SwiftUI integration.32- Read [extended SpriteKit patterns](references/spritekit-patterns.md) for tile maps, atlases, shaders, advanced camera work, audio, and performance recipes.3334## Core Decisions3536- Choose the scene coordinate and scaling contract before positioning content.37- Use category and contact masks as a reviewed collision matrix.38- Keep `SKScene` identity stable when hosted by `SpriteView`.39- Prefer textures and atlases over expensive shape-node rendering in repeated content.4041## Common Mistakes4243### Creating a new scene on every SwiftUI re-render4445```swift46// DON'T: Scene is recreated on every body evaluation47var body: some View {48 SpriteView(scene: GameScene(size: CGSize(width: 390, height: 844)))49}5051// DO: Create once and reuse52@State private var scene = GameScene(size: CGSize(width: 390, height: 844))53var body: some View {54 SpriteView(scene: scene)55}56```5758### Adding a child node that already has a parent5960A node can only have one parent. Remove from the current parent first or61create a separate instance. Adding a node that already has a parent crashes.6263### Forgetting to set contactTestBitMask6465```swift66// DON'T: Bodies collide but didBegin is never called67player.physicsBody?.categoryBitMask = PhysicsCategory.player68enemy.physicsBody?.categoryBitMask = PhysicsCategory.enemy6970// DO: Set contactTestBitMask to receive contact callbacks71player.physicsBody?.contactTestBitMask = PhysicsCategory.enemy72```7374### Using SKShapeNode for performance-critical rendering7576`SKShapeNode` uses a separate draw call per instance. Prefer `SKSpriteNode`77with a texture for repeated elements to enable batched rendering.7879### Not removing nodes that leave the screen8081```swift82// DON'T83enemy.run(SKAction.moveBy(x: -800, y: 0, duration: 3.0))84addChild(enemy)8586// DO: Remove after leaving the visible area87enemy.run(SKAction.sequence([88 SKAction.moveBy(x: -800, y: 0, duration: 3.0),89 SKAction.removeFromParent()90]))91addChild(enemy)92```9394### Setting physicsWorld.contactDelegate too late9596Set `physicsWorld.contactDelegate = self` in `didMove(to:)`, not in97`update(_:)` or after a delay.9899## Review Checklist100101- [ ] Scene subclass overrides `didMove(to:)` for setup, not `init`102- [ ] `scaleMode` chosen appropriately for the game's design103- [ ] `ignoresSiblingOrder` set to `true` on `SKView` for performance104- [ ] `zPosition` used consistently when `ignoresSiblingOrder` is enabled105- [ ] Physics `contactDelegate` set in `didMove(to:)`106- [ ] Category, collision, and contact bit masks configured correctly107- [ ] `contactTestBitMask` set for any pair needing `didBegin`/`didEnd` callbacks108- [ ] Contact callbacks queue changes instead of mutating the physics world directly109- [ ] Static bodies use `isDynamic = false`110- [ ] `SKShapeNode` avoided in performance-critical paths; `SKSpriteNode` preferred111- [ ] Actions that move nodes offscreen include `.removeFromParent()` in sequence112- [ ] One-shot emitters remove themselves after particle lifetime expires113- [ ] Emitter `targetNode` set when particles should stay in world space114- [ ] Scene stored in `@State` when used with `SpriteView` in SwiftUI115- [ ] Texture atlases used for related sprites to reduce draw calls116- [ ] `update(_:)` uses delta time for frame-rate-independent movement117- [ ] Nodes removed from parent before being re-added elsewhere118119## References120121- See [references/spritekit-patterns.md](references/spritekit-patterns.md) for tile maps, texture atlases, shaders,122 scene transitions, game loop patterns, audio, and SceneKit embedding.123- [SpriteKit documentation](https://sosumi.ai/documentation/spritekit)124- [SKScene](https://sosumi.ai/documentation/spritekit/skscene)125- [SKSpriteNode](https://sosumi.ai/documentation/spritekit/skspritenode)126- [SKAction](https://sosumi.ai/documentation/spritekit/skaction)127- [SKPhysicsBody](https://sosumi.ai/documentation/spritekit/skphysicsbody)128- [SKEmitterNode](https://sosumi.ai/documentation/spritekit/skemitternode)129- [SKCameraNode](https://sosumi.ai/documentation/spritekit/skcameranode)130- [SpriteView](https://sosumi.ai/documentation/spritekit/spriteview)131- [SKTileMapNode](https://sosumi.ai/documentation/spritekit/sktilemapnode)132- [Core implementation details](references/core-implementation.md) -- setup, API wiring, and focused implementation recipes moved out of the entrypoint.