11

What effect addChild and zPosition really have on a scene?

I mean this: suppose I do this

sprite1.zPosition = 50;
sprite2.zPosition = 10;
sprite3.zPosition = 30;

and later do this

[self addChild:sprite1];
[self addChild:sprite2];
[self addChild:sprite3];

according to SpriteKit and considering zPosition order, sprite1 should be rendered over sprite3 over sprite2 but spriteKit ignores that and renders 3 over 2 over 1.

am I missing something?

Duck
  • 34,902
  • 47
  • 248
  • 470
  • no more code. The code is just this. 3 layers with 3 different zPositions but are added in random order. SpriteKit should respect zPosition but it is not. – Duck Feb 11 '14 at 18:15
  • Try setting zPosition after addChild instead of before. – user688518 Feb 11 '14 at 18:25
  • That should do no difference. – Dobroćudni Tapir Feb 11 '14 at 18:26
  • Can you make a test project for this case? Or just paste entire code from `SKScene` – Andrey Gordeev Feb 12 '14 at 04:34
  • It would help if you showed more code and then a screen shot of what is happening vs what should be happening. – Fogmeister Feb 12 '14 at 08:17
  • You weren't by any chance using an SKCropNode, were you? I just ran into the same issue when adding children to one of them, sprites seemed like they were rendered at a random depth and the same code would result in different results despite all the zPositions being the same. I've gotten it to work by creating an empty node, adding it as the only child of my SKCropNode instance, and then adding all my other nodes to that node instead of the SKCropNode instance, but I definitely don't think I should have to do that. – Devin Jun 03 '14 at 00:58
  • I'm seeing all kinds of unpredictable behavior concerning the parent/child order and the zPositions. I suspect SpriteKit is buggy here (too). iOS 9.2. – Jonny Feb 04 '16 at 10:20

2 Answers2

16

If they have the same parent that would be true. In case they can different parents, the z order of parents is also taken into account.

The standard behavior for scene rendering follows a simple pair of rules:

  1. A parent draws its content before rendering its children.
  2. Children are rendered in the order in which they appear in the child array.

When you take z positions into account, here is how the node tree is rendered:

  1. Each node’s global z position is calculated.
  2. Nodes are drawn in order from smallest z value to largest z value.
  3. If two nodes share the same z value, ancestors are rendered first, and siblings are rendered in child order.

You can find it well explained here under Understanding the Drawing Order for a Node Tree

Dobroćudni Tapir
  • 3,082
  • 20
  • 37
  • what will be true? 321 or 132? Spritekit is ignoring zPosition and rendering it 321. – Duck Feb 11 '14 at 18:10
  • 123 should be the result. As Jerome suggested we need more code to offer a better answer. – Dobroćudni Tapir Feb 11 '14 at 18:11
  • 123? you mean 1 on top or on bottom? so, what matters is the addChild order, not zPosition. – Duck Feb 11 '14 at 18:17
  • Please read the document I posted under "here". Z position matters, but if 2 nodes have same z position, add order is take into account. What you are experiencing is weird and could be caused by something else. – Dobroćudni Tapir Feb 11 '14 at 18:23
  • Sprite Kit draws all nodes with the same parent in order they were added to the parent, zPosition overrides this order. If the nodes have different parents, zPosition has no effect. – CodeSmile Feb 11 '14 at 20:42
6

In your game view controller, set the ignoresSiblingOrder property to false (or NO in objective-C).

let skView = self.view as SKView
skView.ignoresSiblingOrder = false
Donn
  • 1,680
  • 14
  • 17