How do I set the layer order of SKSpriteNode objects?

SKSpriteNode instances I create are not laid in the order I created them. How do I ensure one sprite is on top of another?

Accepted Reply

Nodes have position in the X, Y and Z direction, where Z is the direction that rises from the screen towards your eye. To order nodes in the Z direction, you set their "zPosition" property to whatever you want. For example, a node at zPosition 0.1 displays above a node at zPosition 0.002.


The values order the nodes top to bottom (or front to back, if you prefer to think of it that way), but they don't really have any other effect because SpriteKit is a 2D system. However, if you any of the pseudo-3D capabilities of SpriteKit (lighting, shadows and perhaps the physics fields), there might actually be a calculation in 3D coordinates for things that are eventually drawn on the screen's 2D surface, so the zPosition values may in fact matter beyond their relative ordering.

Replies

Nodes have position in the X, Y and Z direction, where Z is the direction that rises from the screen towards your eye. To order nodes in the Z direction, you set their "zPosition" property to whatever you want. For example, a node at zPosition 0.1 displays above a node at zPosition 0.002.


The values order the nodes top to bottom (or front to back, if you prefer to think of it that way), but they don't really have any other effect because SpriteKit is a 2D system. However, if you any of the pseudo-3D capabilities of SpriteKit (lighting, shadows and perhaps the physics fields), there might actually be a calculation in 3D coordinates for things that are eventually drawn on the screen's 2D surface, so the zPosition values may in fact matter beyond their relative ordering.

Thank you so much.