Search code examples
swiftsprite-kitskspritenodeskscene

SKSpriteNode causes scene to be grayed out


I create a SKSpriteNode and add it to the scene graph inside my SKScene:

// First I set a white background
self.backgroundColor = SKColor.whiteColor()
let cannon = SKSpriteNode(imageNamed: "cannon")
cannon.size = CGSizeMake(32, 160)
cannon.position = CGPointMake(size.width / 2.0, 0)
self.addChild(cannon)

This is "cannon.png", which is in my Images.xcassets (my bad, it's a very ugly cannon):

enter image description here

If I add it I get a totally gray scene, while if I comment the last line (so I don't add the sprite to the scene), I get a white background.


Solution

  • You need to initialize the size of your scene before adding the nodes to it. Modify your init method like this and problem will be solved.

    let scene = GameScene(size: view.bounds.size)