sprite-kitswift2zposition

SKSpriteNodeChild in the wrong zPosition


I'm adding two SKSpriteNodes as children to another SKSpriteNode. The code looks like this:

let parentNode = SKSpriteNode(imageNamed: "ParentImage")
parentNode.position = CGPointMake(0,0)
parentNode.zPosition = 0
let child1 = SKSpriteNode(imageNamed: "ChildImage1")
child1.position = CGPointMake(0,0)
parentNode.addChild(child1)
child1.zPosition = -10
let child2 = SKSpriteNode(imageNamed: "ChildImage2")
child2.position = CGPointMake(0,0)
parentNode.addChild(child2)
child2.zPosition = 10

When I add the parentNode to my scene, the order of sprites in the z-plane, from back-to-front is

and sometimes, the expected

Does the zPosition property get over-ruled when a child is below the parentNode? Has anyone come across a bug like this or know of a fix?

In the GameViewController, skView.ignoresSiblingOrder = true When I set it to false, I get the first behaviour (parentNode, child1, child2) every time.


Solution

  • The problem has been fixed. I can't be 100% sure what solved it as I tried a few things. I set the ignoresSiblingOrder to false and then back to true, moved the definition of the childNode that was appearing on top into the same function as the parent, and, finally (I suspect this was the reason for the fix), moved the order of addChild and setting the zPosition (in my project, the order was originally child1.zPosition, parentNode.addChild; changing the order of these two probably fixed the problem as I suspect the subsequent sibling order was confusing the zPosition).