SKSpriteNode is hiding below the parent node

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) 
+5
source share
1 answer

What you are looking for is the ignoresSiblingOrder property on SKView :

Boolean value indicating parent-child and sibling relationships affect the rendering order of nodes in the scene.

By default, Xcode 6.x (and maybe 5, I didn’t check), the SpriteKit template sets true when setting up SKView in the view controller. When set to true , the order in which nodes are added to the scene does not affect their z-position. If you set it to false , they will be layered in the order that they were added to the scene (or the parent node).

At the same time, SpritKit can do some optimizations if ignoresSiblingOrder is true (therefore, this is the default value in the template), so it is probably best to save it this way, if at all possible. In this case, you will have to manually set the zPosition property for each node.

+9
source

Source: https://habr.com/ru/post/1203916/


All Articles