Display Background with SpriteKit iOS Tutorial

Sprite Kit provides a hardware-accelerated animation system which has been optimized for creating 2D games. In this tutorial we will use the Game template in Xcode to add a background image. This tutorial is made with Xcode 10 and built for iOS 12.

Open Xcode and choose iOS -> Application -> Game

For product name, use IOSSpriteKitBackgroundTutorial and then fill out the Organization Name and Organization Identifier with your customary values. Enter Swift as Language and SpriteKit as Game Technology.

We need a background for this tutorial so download the image. Open the Assets folder and drag the image to it.

Go to the GameScene.sks file and delete the hello World label. Select the Scene and in the Attributes inspector change the X and Y values of the Anchor point to 0.

The Game template added some code to the project we will not need. Go to the GameScene.swift file and delete all code inside the GameScene class. Add the didMove(to:) method to the empty GameScene class.

override func didMove(to view: SKView) {
    let background = SKSpriteNode(imageNamed: "background.jpg")
    background.position = CGPoint(x: size.width/2, y: size.height/2)

Sprite Kit has a special class to create and manipulate sprites called SKSpriteNode. In this node our image is loaded. Next, the image is added to our scene.

Build and Run the project.

You can download the source code of the IOSSpriteKitBackgroundTutorial at the ioscreator repository on github.