All articles

Intermediate

Fly in Any Direction in SpriteKit with Skypirates

Steer a plane across an open map with a touch joystick or keyboard, and let the camera follow it horizontally, vertically, and diagonally.

Open Skypirates.xcodeproj from the sample ZIP and run it on an iPhone or iPad simulator. Drag the left joystick or use WASD or the arrow keys. When you release steering, the plane keeps flying in its last direction.

Skypirates flying over an ocean dotted with islands, with a touch joystick at the lower left.
Skypirates running in the iPhone simulator.

Separate the viewport from the world

Open Levels/Skypirates.plist in the sample’s LevelEditor workspace. Its scroll orientation is both, and the design scene is 960 × 540 points. The iOS demo uses that size for the view and HUD. LevelData.worldBounds defines a larger 3,200 × 3,200-point playable area. Islands and enemies stay at their world coordinates as the camera moves.

Turn a steering vector into a heading

In GameScene.swift, the joystick measures the touch relative to its center. The knob stops at its 42-point travel limit, and dividing its position by 42 gives the game a steering vector. Keyboard directions feed into the same vector. A small dead zone filters touches near the center.

The artwork faces up at zero radians. The demo uses atan2(-x, y) to aim the nose toward the input, and its turn helper takes the shortest route to the new heading. The plane moves forward at 195 points per second. Diagonal input changes its direction while its speed stays constant.

let desired = input()
if hypot(desired.x, desired.y) > 0.16 {
    heading = turn(heading, toward: angle(to: desired), rate: CGFloat(delta) * 4.2)
}
let forward = direction(heading)
player.zRotation = heading
player.position.x += forward.x * 195 * delta
player.position.y += forward.y * 195 * delta

The next lines clamp the plane to the playable area with an inset. The demo also uses the heading to fire along the nose.

Follow the plane on both axes

Construct CameraFollowHelper with the plane as its target, the scene size as its viewport, and the world rectangle as its bounds.

follow = CameraFollowHelper(camera: lens, target: player,
                            viewportSize: size, bounds: data.worldBounds)
follow.followsHorizontal = true
follow.followsVertical = true
follow.smoothing = 7

After simulation, the demo points the camera’s look-ahead 70 points along the plane’s heading and updates the helper. The camera stays upright as the plane turns. At a world edge, the helper clamps the camera, and the plane can move away from the center of the screen. The clouds use a separate offset to create parallax.

Tune how far ahead the camera looks

In Skypirates iOS Demo/Skypirates/GameScene.swift, find finishFrame(). Change the 70-point offset to 110 and run the same turn near the middle of the map. The plane should sit farther back in the viewport, leaving more room ahead. Set it to zero to see how centered follow feels.

let forward = direction(heading)
follow.lookAhead = CGVector(dx: forward.x * 110, dy: forward.y * 110)
follow.viewportSize = size
follow.update(deltaTime: delta)

Try diagonal flight and a map edge

Hold Up and Right together, then release both keys. Watch the plane turn and continue along its heading. Fly toward a corner and watch the camera stop at the boundary. To see how world placement affects the route, move an island in LevelEditor, save, and run the sample’s sync-level.sh script before rebuilding.

See how to add parallax to a scrolling scene

Where Scroller2d saves work

The plane’s steering stays in the game. Scroller2d handles the camera follow, viewport bounds, and smoothing. This comparison isolates clamping an unsmoothed camera on both axes, with a viewport smaller than the world and shared coordinates.

SpriteKit camera clamp (5 lines)

let halfWidth = size.width / 2
let halfHeight = size.height / 2
let x = min(bounds.maxX - halfWidth, max(bounds.minX + halfWidth, plane.position.x))
let y = min(bounds.maxY - halfHeight, max(bounds.minY + halfHeight, plane.position.y))
camera.position = CGPoint(x: x, y: y)

With Scroller2d (3 lines)

let follow = CameraFollowHelper(camera: camera, target: plane, viewportSize: size, bounds: bounds)
follow.followsVertical = true
follow.update(deltaTime: delta)

Create the helper once and keep it on the scene. During each frame, call only update. The excerpts omit smoothing and look-ahead.