Update note: This SpriteKit tutorial has been updated by Brody Eller. The original post was written by Ray Wenderlich.
Like Batman and Robin or Superman and Lois Lane, SpriteKit and Swift are an amazing combination:
In this tutorial, you will learn how to create a simple 2D game using Apple’s 2D game framework, SpriteKit — using Swift!
You can either follow along with this SpriteKit tutorial, or just jump straight to the sample project at the end. And yes. There will be ninjas.
The most popular alternative to SpriteKit at the moment is a game framework called Unity. Unity was originally developed as a 3D engine, but it has full built-in 2D support, too.
Before you get started, put some thought into whether SpriteKit or Unity is the best choice for your game.
Advantages of SpriteKit
Advantages of Unity
After this you may be thinking, “Well, which 2D framework should I choose?”
The answer depends on what your goals are:
If you think Unity is for you, check out our book Unity Games by Tutorials.
Otherwise, keep reading on to get started with this SpriteKit tutorial!
Start by downloading the project files at the top or bottom of this page. Open the starter project and navigate to GameViewController.swift. Add the following at the end of viewDidLoad():
let scene = GameScene(size: view.bounds.size)
let skView = view as! SKView
skView.showsFPS = true
skView.showsNodeCount = true
skView.ignoresSiblingOrder = true
scene.scaleMode = .resizeFill
skView.presentScene(scene)
GameViewController is a normal UIViewController, except that its root view is an SKView, which is a view that contains a SpriteKit scene.
Here, you’ve instructed viewDidLoad() to create a new instance of the GameScene on startup, with the same size as the view itself. Setting skView.showsFPS to true means a frame rate indicator will be displayed.
That’s it for the initial setup; now it’s time to get something on the screen!
Open GameScene.swift and add with the following to GameScene:
let player = SKSpriteNode(imageNamed: "player")
override func didMove(to view: SKView) {
backgroundColor = SKColor.white
player.position = CGPoint(x: size.width * 0.1, y: size.height * 0.5)
addChild(player)
}
Here’s what this does, step by step:
Build and run, and voilà — ladies and gentlemen, the ninja has entered the building!

Next you want to add some monsters into your scene for your ninja to combat. To make things more interesting, you want the monsters to be moving — otherwise there wouldn’t be much of a challenge! You’ll create the monsters slightly off screen to the right and set up an action that tells them to move to the left.
Add the following methods to GameScene.swift, inside the class, before the closing curly brace:
func random() -> CGFloat {
return CGFloat(Float(arc4random()) / 0xFFFFFFFF)
}
func random(min: CGFloat, max: CGFloat) -> CGFloat {
return random() * (max - min) + min
}
func addMonster() {
let monster = SKSpriteNode(imageNamed: "monster")
let actualY = random(min: monster.size.height/2, max: size.height - monster.size.height/2)
monster.position = CGPoint(x: size.width + monster.size.width/2, y: actualY)
addChild(monster)
let actualDuration = random(min: CGFloat(2.0), max: CGFloat(4.0))
let actionMove = SKAction.move(to: CGPoint(x: -monster.size.width/2, y: actualY),
duration: TimeInterval(actualDuration))
let actionMoveDone = SKAction.removeFromParent()
monster.run(SKAction.sequence([actionMove, actionMoveDone]))
}
The first part of addMonster() should make sense based on what you’ve learned so far: you do some simple calculations to determine where you want to create the object, set the position of the object, and add it to the scene the same way you did for the player sprite.
The new element here is adding actions. SpriteKit provides a lot of extremely useful built-in actions that help you easily change the state of sprites over time, such as move actions, rotate actions, fade actions, animation actions, and more. Here you use three actions on the monster:
Note: This code block includes some helper methods to generate a random number within a range using arc4random(). This suffices for the simple random number generation needs in this game, but if you want more advanced functionality, check out the random number APIs in GameplayKit.
One last thing before you move on. You need to actually call the method to create monsters! And to make things fun, let’s have monsters continuously spawning over time.
Simply add the following code to the end of didMove(to:):
run(SKAction.repeatForever(
SKAction.sequence([
SKAction.run(addMonster),
SKAction.wait(forDuration: 1.0)
])
))
Here you run a sequence of actions to call a block of code, and then wait for 1 second. You repeat this sequence of actions endlessly.
That’s it! Build and run the project; now you should see monsters happily moving across the screen:
