harreskhan harreskhan - 3 months ago 11x
Swift Question

Swift Spritekit Background image

I've recently started learning swift and sprite kit. i am currently trying to do the basics and was trying to fit a background image on to the screen. i have managed to get the background picture up and in the centre of the screen but it is not filling up the whole screen. all help would be appreciated.

import SpriteKit

class GameScene: SKScene {
override func didMoveToView(view: SKView) {
/* Setup your scene here */

let bgImage = SKSpriteNode(imageNamed: "background.png")
bgImage.position = CGPoint(x:self.size.width/2, y: self.size.height/2)
bgImage.anchorPoint = CGPointMake(0.5, 0.5)
bgImage.zPosition = -1


You can change the size of your sprite to fit the size the scene

bgImage.size = self.size

Of course if the proportions of the scene are different from the proportions of the sprite, the sprite will be stretched.

Thank you to @Knight0fDragon for the helpful comments below.