Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SpriteKit not respecting zPosition?

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?

like image 938
Duck Avatar asked Feb 11 '14 17:02

Duck


1 Answers

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

like image 68
Dobroćudni Tapir Avatar answered Oct 19 '22 17:10

Dobroćudni Tapir