All articles

Build Parallax Backgrounds for a Scrolling SpriteKit Game

Combine camera movement and repeating image layers to give a level depth.

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.

Why a single background feels flat

When every background object follows the same movement, the scene has few visual cues for depth. Parallax uses different apparent movement rates for layers behind the player. Repeating layers need enough coverage to avoid exposing an empty area as the camera advances.

Describe the layers in level data

Desert Ruins reads the level’s background definitions and creates three tiles for each visible layer. The sample stores each layer’s tiles and configured speed in BackgroundLayer and attaches the tiles to the camera. That makes their positions relative to the view while gameplay objects remain in the world.

Wrap the layer offset

CameraFollowHelper now handles following and bounds. The sample calls it from refreshCamera() after physics, then wraps each background layer using the camera's resulting position. Three adjacent camera-child tiles cover the view as the shift changes:

cameraFollow?.viewportSize = size
cameraFollow?.update(deltaTime: frameDelta)
for layer in backdrop {
    let shift = (lens.position.x * layer.speed)
        .truncatingRemainder(dividingBy: size.width)
    for (index, tile) in layer.tiles.enumerated() {
        tile.position.x = CGFloat(index - 1) * size.width - shift
    }
}

The demo configures a 90-point look-ahead and leaves smoothing at zero to preserve its original camera movement. Other games can set smoothing to an exponential response per second and enable followsVertical. Supply visible world dimensions as viewportSize, including camera zoom, and refresh them when the view changes. Camera bounds use camera-parent coordinates; keep the camera unrotated.

Prepare images for repetition

The left and right edges of a layer should join cleanly. A visible edge in the source image becomes a seam every time the layer wraps. Keep the tile size and the wrap distance in agreement. This sample uses scene-width tiles; adapting it to another tile width also means changing the spacing and remainder distance.

Tune while watching the whole level

Give distant layers a gentler apparent motion and test the foreground against them. Walk across several wrap distances, stop, and reverse direction. Check both seams and the camera’s final clamp. The editor supplies layer data, the API supplies camera following and limits, and the demo supplies repeating-tile placement. Parallax assembly remains game code.

Run and edit Desert Ruins

Where Scroller2d saves work

The camera helper gives each level the same follow and clamping behavior. Background entries let designers change the image and scroll factor independently. Desert Ruins still wraps the repeated tiles in game code, so that wrapping can be shared while the layer values vary by level.