2D Camera in SpriteKit

One thing I missed when I started working with SpriteKit is a proper camera system. In my old, OpenGL ES-based game framework I had a meticulously built 2D camera system that I used literally all of the time; full-on game engines like Unity or Unreal usually have a 2- or 3D camera as a first-order primitive.

Apple’s SpriteKit docs hint at a solution (and give a partial implementation in Objective-C) — attach an empty node to your world and keep the view centred on its position. Below is a complete Swift implementation that sets up a 2d world with its origin at the centre of the screen, plus a camera to move the view within the world and a fixed overlay for showing HUD elements:

Change what’s visible in the world by moving the camera:

3 thoughts on “2D Camera in SpriteKit

Leave a Reply

Your email address will not be published. Required fields are marked *