Search code examples
swiftsprite-kitskscene

How to change the background colour of SKScene?


I have a class based on SKScene. As we know to change bg colour we have to use backgroundColor property with UIColor value.

Unfortunately my code self.backgroundColor = UIColor(red: 40.0, green:40.0, blue:40.0, alpha:1.0) doesn't work, instead of expected result (dark grey colour) I get white (it looks that something went wrong and it uses the default colour)

I was trying to add self.view.allowTransparent = true it haven't helped too.

Also my viewcontroller has my expected colour which was set in storyboard and self.backgroundColor = .clear also fill it with white.

How to change SKScene background colour for custom or for transparent colour?


Solution

  • The problem is that you are trying to set color in 8 bit 255 rgb, but UIColor accepts floating point value eg 0 ... 1.0.

    Transform your values to floating point values.

    self.backgroundColor = UIColor(red: 0.157, green: 0.157, blue: 0.157, alpha: 1.0)