Using Swift and SpriteKit, I have a problem with SKSpriteNode, which does not appear when added as a child to another SKSpriteNode. In contrast, an SKLabelNode placed in exactly the same place appears.
// does not show up, expected a white square override func renderBody(parentNode: SKNode, position: CGPoint) { let node = SKSpriteNode(color: UIColor.whiteColor(), size: CGSize(width: 48, height: 48)) node.position = position parentNode.addChild(node) } // does show up as white text in the correct position override func renderBody(parentNode: SKNode, position: CGPoint) { let node = SKLabelNode(text: "hello") node.position = position parentNode.addChild(node) }
'parentNode' and 'position' are the same in both cases.
'parentNode' is a larger SKSpriteNode created with a dark texture that covers the screen as a background.
'position' - CGPoint (x: 50, y: 50).
It seems that the sprite of the rectangle falls under the background, while the sprite label hits the top. I experimented by removing the background and adding a rectangle sprite directly to the node scene, and then it will appear.
Does anyone have any idea what might be wrong?
Edit: manually setting zPosition solves the problem. However, I still think something is wrong. You do not need to set it manually for a child of a node to avoid hiding the parent of a node. I never had to do this in Xcode 5. And why is there a difference between labelnode and node sprite?
Here is an example that I hope illustrates this better. Create the default SpriteKit, Swift, Iphone project in Xcode 6. Paste the code below in the for-loop in touchhesBegan. Remove the Hello World shortcut created in didMoveToView. Launch it and click somewhere in the middle of the screen.
let location = touch.locationInNode(self) // LabelNode without manually setting zposition // visible let backGround1 = SKSpriteNode(imageNamed:"Spaceship") backGround1.position = CGPoint(x: location.x, y: location.y + 250) backGround1.xScale = 0.5 backGround1.yScale = 0.5 self.addChild(backGround1) let labelNode = SKLabelNode(fontNamed: "ChalkDuster") labelNode.text = "I am on top" labelNode.fontSize = 48 labelNode.fontColor = UIColor.yellowColor() backGround1.addChild(labelNode) // SpriteNode without manually setting zposition // hidden under its parent node let backGround2 = SKSpriteNode(imageNamed:"Spaceship") backGround2.position = location backGround2.xScale = 0.5 backGround2.yScale = 0.5 self.addChild(backGround2) let spriteNode2 = SKSpriteNode(color: UIColor.yellowColor(), size: CGSize(width: 100, height: 100)) backGround2.addChild(spriteNode2) // SpriteNode with zposition manually set // visible let backGround3 = SKSpriteNode(imageNamed:"Spaceship") backGround3.position = CGPoint(x: location.x, y: location.y - 250) backGround3.xScale = 0.5 backGround3.yScale = 0.5 self.addChild(backGround3) let spriteNode3 = SKSpriteNode(color: UIColor.yellowColor(), size: CGSize(width: 100, height: 100)) spriteNode3.zPosition = 0.001 backGround3.addChild(spriteNode3)