Drawing order for a node tree
When a node tree renders, all its children also render. First, the parent is rendered, and then, its children, in the order they are added to parent. If you have many nodes to render in a scene, it is a difficult task to maintain them in order. For this, Sprite Kit provides a solution using the z position. You can set nodes to the z position by using the zPosition
property.
When you take the z position into account, the node tree will be rendered as follows:
First of all, each node's global z position is calculated
Then, nodes are drawn in order from smallest z value to largest z value
If two nodes share the same z value, ancestors are rendered first, and siblings are rendered in child order
As you've just seen, Sprite Kit uses a deterministic rendering order, based on the height nodes and their positions in the node tree. But, because the rendering order is so deterministic, Sprite Kit may be unable to apply some rendering optimizations that it might otherwise apply...