Tr Hoangtien Tr Hoangtien - 6 months ago 11
iOS Question

How can I set Background Color for a Scene?

I have put

super.viewDidLoad()
self.view.backgroundColor = UIColor.yellowColor()


in

override func viewDidLoad() {

}


of GameViewController.swift. But the background doesn't change to yellow. Then, I even add that code to:

override func didMoveToView(view: SKView) {

}


But still, the background doesn't change.

Any way can I change the color of my scene's background ?

Answer

You need to set the background color of the scene not background color of your view controller's view:

// let's create a test Scene

public func setupScene() -> SKScene {
    let scene = SKScene(size: CGSize(width: 1024, height: 768))
    scene.scaleMode = .AspectFit    // define the scaleMode for this scene
    scene.backgroundColor = SKColor.lightGrayColor()   // HERE: background color

    return scene
}

EDIT: To set a "background image", you just add a SKSpriteNode to the scene

// let's create a test Scene

public func setupScene() -> SKScene {
    let scene = SKScene(size: CGSize(width: 1024, height: 768))
    scene.scaleMode = .AspectFit    // define the scaleMode for this scene

    let n = SKSpriteNode(imageNamed: "background.png")
    scene.addChild(n)
    return scene
}