SpriteKit not respecting zPosition?
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:
- A parent draws its content before rendering its children.
- 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:
- Each node’s global z position is calculated.
- 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.
You can find it well explained here under Understanding the Drawing Order for a Node Tree
SpriteKit issue with zPosition
Here is how you are expecting your code right now
-100: Node
# 0: Node
# 0: Blue
0 : Node
# 0: Node
# 0:Red
100 : Node
# 0: Node
# -200:Green
# -150:Yellow
But what is really happening is this
-100: Node
: Node
: Blue
: Green
-50 : Yellow
0 : Node
: Node
: Red
100 : Node
: Node
This is because the zPosition is just subtracted from the parent, and the screen then draws in 1 pass all the nodes.
To combat this, you need to create a custom class that overrides the addChild
function and do somethings that keep track of your zPosition
in another variable (probably a dictionary), then set the child zPosition to zero, and use insertChild(node:, atIndex:)
to place your children in order in the children array using this new variable to preserve your order. Then if you are going even more crazy, you would have to keep track of when a child changes its zPosition, and use the above style to set it correctly in the array
SKLabelNode with Children not affected by gravity - not respecting zPosition
Assuming that skView.ignoresSiblingOrder = true
in the view controller (the default setting), some thoughts...
- When
ignoresSiblingOrder
is set totrue
, Sprite Kit ignores sibling relationships when determining the order in which to draw nodes. It does not, however, affect the parent-sibling draw order. - In either mode, the parent is drawn first and then the child nodes are rendered. Normally, the children appear on top of the parent (if they're at the same location) regardless of the
zPosition
s of the parent and children! The exception is if a child'szPosition
is less than zero. In this case, the child will appear beneath its parent (even if the parent'szPosition
is less than the child). - It doesn't appear that you are setting
node.node.name
nor thename
of the sprite node. If the names aren't set, theif
condition will fail and the physics body won't be attached tonode.node
. - The default settings for shapes only draws a white border around its
path
(in this case, a rectangle). You should see this as an white outline around the sprite node. Settingbackground.fillColor = SKColor.greenColor()
will show theSKShapeNode
filled with green.
SpriteKit - The child of my node does not appear to be changing its zPosition attribute
So, it turns out I was setting the zPosition for the shield in its method. Removing that restores normal behaviour when the parent changes zPosition, having it declared explicitly seems to override the nested behaviour.
SpriteKit grand-children of views overlap zPosition
well according to this SO answer, indeed all the zPosition values are calculated before drawing.
I ended up creating a counter for bubbles, adding 1 every time a bubble has been added, and assinging the counter value as its zPosition.
And inside the bubbles, I made sure every child has a zPosition in the range (0, 1)
SpriteKit: SKPhysicsJointLimit not respecting 'maxLength'
Be sure that the anchor points are in scene coordinates, as described in the documentation. The (0.5, 0.5) is likely intended to be "center of the sprite" or something like that, but that's not correct for a joint.
change zPosition of tiles
There is nothing in the definition that allows for zPosition change (Which would make sense since the idea is to draw the tiles as fast as possible)
I would recommend having another tile set overlay it, or have individual sprites that pop out.
Related Topics
iOS 13.2 Message: Nehelper Sent Invalid Result Code [1] for Wi-Fi Information Request
Afnetworking Ntlm Authentication
Uitableviewcells Initial Load View/Display Issue
Preventing Erasure of User Data While Upgrading iOS Application via Itunes
How to Print Out(Nslog) The Properties of a Custom Object Added to a Nsmutablearray
How to Programmatically Wrap Png Texture Around Cube in Scenekit
Start Position of Table View Is Wrong in Simulator, and Is Not The Same in Storyboard
Can Someone Explain This @Synthesize Syntax
Two Button Simultaneous Press Input
How to Animate Dark Mode Change in iOS
Duplicate Collectionview Cells After Reloaddata with Firebase Query
Implementing Codable for UIcolor
How to Fix Crash When Tap to Select Row After Scrolling The Tableview
Scenekit Flattenedclone - Incorrect Use of Objc_Storeweak() and Objc_Loadweak() Error