简体   繁体   中英

How do I change background colour programmatically in swift using SpriteKit

I have this game and I have a function for when each different game mode is called, I want to change the colour of the background when a mode is clicked, but when I do It nothing happens. Could you please tell me how to do it, Thanks

override func update(_ currentTime: TimeInterval) {
    // Called before each frame is rendered


    switch currentGameType {
    case .easy:
        enemy.run(SKAction.moveTo(x: ball.position.x, duration: 1.3))
        break
    case .medium:
        enemy.run(SKAction.moveTo(x: ball.position.x, duration: 1.0))
        self.view?.backgroundColor = UIColor.white //I TRY IT HERE
        break
    case .hard:
        enemy.run(SKAction.moveTo(x: ball.position.x, duration: 0.7))
        break
    case .player2:

        break
    }

Your scene's background has nothing to do with the view's background.

You should set the scene's background color instead. The scene, in this case, is self :

self.backgroundColor = .white

However, if you want to use an image for the background, you need to add a new node covering the whole scene and set the texture of the node to be your image.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM