I'm trying to add a SKShapeNode on the scene, but if I use this:
let rect = SKShapeNode(SKShapeNode(rect: CGRectMake(0, 0, 100, 100)))
self.addChild(rect)
This rectangle is added on the last layer and overlaps other nodes. So, how to add it on the first level?
Thanks in advance
You need to set the zPosition
property of your SKNode
. From the docs:
The default value is 0.0. The positive z axis is projected toward the viewer so that nodes with larger z values are closer to the viewer. When a node tree is rendered, the height of each node (in absolute coordinates) is calculated and then all nodes in the tree are rendered from smallest z value to largest z value. If multiple nodes share the same z position, those nodes are sorted so that parent nodes are drawn before their children, and siblings are rendered in the order that they appear in their parent’s children array. Hit-testing is processed in the opposite order.
The SKView class’s ignoresSiblingOrder property controls whether node sorting is enabled for nodes at the same z position.
Since you seem to want your rect
node under your other nodes, and since the default zPosition
is 0.0
, try setting it to -1.0
:
let rect = SKShapeNode(SKShapeNode(rect: CGRectMake(0, 0, 100, 100)))
rect.zPosition = -1.0
self.addChild(rect)