Landon
Landon

Reputation: 359

How do I add a background to my scene? SpriteKit Swift 4

I am making a endless runner game using SpriteKit. When I run the game, the background is black, and I've tried 3 methods on how to change the background, but it still is black. The code for my scene is

if let view = self.view as! SKView? {
   // Load the SKScene from 'GameScene.sks'
   let scene = GameScene(size: view.frame.size)
   scene.scaleMode = .aspectFill
   view.presentScene(scene)

   view.ignoresSiblingOrder = true
   view.showsFPS = true
   view.showsNodeCount = true
}

I want to replace the black background with this:

background

Does anyone know how to solve this?

Upvotes: 1

Views: 450

Answers (2)

Alim2
Alim2

Reputation: 41

If you have an image you want to make be your background, make a SKSpriteNode object with parameters that take in the name of your image within your assets.xcassets folder.

If your background doesnt fill the whole screen, use the setScale(multiplier) function on it to enlarge it. Resolution might be affected depending on how high resolution your image is/how large it is to start with.

Then add the SKSpriteNode with the addChild(SKSpriteNode) function. Ideally do this last part within your didMove(to:view) function.

Upvotes: 0

peacetype
peacetype

Reputation: 2068

In your GameScene.swift file, put the following code in your didMove(to:) method:

self.backgroundColor = UIColor(red: 199.0/255.0, green: 246.0/255.0, blue: 248.0/255.0, alpha: 1.0)

Also make sure that you are not already setting the background color in some other way.

Upvotes: 1

Related Questions