简体   繁体   中英

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):

在此处输入图片说明

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.

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)

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