Problem with SpriteKit with zPosition

I ran into a problem with zPosition nodes in my SpriteKit scene.

I am creating a node dispatcher that allows you to "impose" like "managing zPosition nodes, essentially letting you get rid of the manual control of the zPosition property and instead add nodes using type methods addNodeUnder(node:SKNode), as well as create groups.

Here is the test scene I did:
We can see the blue node, the green node itself, under the yellow node itself, under the red node itself.

The problem is that with the actions I performed, the green and yellow node should be on top of the red node.

I made my own little debugger, displaying the SceneKit scene graph in hierarchical order:

Node of type LMManagerNode  has zPosition = 0.0 and has children :
           Node of type LMWarpperNode  has zPosition = 0.0 and has children :
                     Node of type SKSpriteNode ('Red node') has zPosition = 0.0.

           Node of type LMWarpperNode  has zPosition = -100.0 and has children :
                     Node of type SKSpriteNode ('Blue node') has zPosition = 0.0.

           Node of type LMWarpperNode  has zPosition = 100.0 and has children :
                     Node of type LMGroupNode ('Test group') has zPosition = 0.0 and has children :
                               Node of type LMWarpperNode  has zPosition = -150.0 and has children :
                                         Node of type SKSpriteNode ('Yellow node') has zPosition = 0.0.

                               Node of type LMWarpperNode  has zPosition = -200.0 and has children :
                                         Node of type SKSpriteNode ('Green node') has zPosition = 0.0.

As you can see:

  • The red node is contained inside the node, which has zPosition = 0
  • The blue node is contained inside the node, which has zPosition = -100
  • node, node, zPosition = 100

node, node, zPosition = 0, node, , zPosition = 100, node?

ignoresSiblingOrder SKView false, ...

EDIT: : yellowNode.zPosition = 1000 node node. ?

EDIT # 2 :

func renderNodeHieararchyForNode(node:SKNode, index:Int) {

    var i = 0
    var beginning = ""
    while i != index {
        beginning += "          "
        i++
        if i == index {
            beginning += " "
        }
    }

    print("\(beginning)Node of type \(node.dynamicType) \(node.name != nil ? "('\(node.name!)')" : "") has zPosition = \(node.zPosition)\(node.children.count > 0 ? " and has children :" : ".")")
    for (i,child) in node.children.enumerate() {
        renderNodeHieararchyForNode(child, index: index+1)

        if i == node.children.count-1 {
            print("")
        }
    }

}

func renderNodeHiearchyForNode(node:SKNode) {
    renderNodeHieararchyForNode(node, index: 0)
}

, SpriteKit , .

+4
2

 -100: Node
     # 0: Node
        # 0: Blue
   0 : Node
     # 0: Node
        # 0:Red
 100 : Node
     # 0: Node
        # -200:Green
        # -150:Yellow

,

 -100: Node
     : Node
     : Blue
     : Green
 -50 : Yellow
   0 : Node
     : Node
     : Red
 100 : Node
     : Node

, zPosition , 1 .

, , addChild -, zPosition (, ), zPosition insertChild(node:, atIndex:), children, , . , , , zPosition, ,

+2

Apple,

z node s node, node x y . , z node .

z , node:

  • z node s.
  • z z.
  • z, , .

node global z position = node z position + node parent z position + node parent parent z position + ...

z

red is 0        (0 + 0 + 0)
blue is -100    (0 + -100 + 0)
yellow = -50    (-150 + 0 + 100 + 0)
green = -100    (-200 + 0 + 100 + 0)

z ( 1 2) , ,

red
yellow
green / blue

, - /. z, ( 3). LMWarpperNode node (.. ) LMManagerNode node LMWarpperNode node, , .

()

red                0
yellow           -50
green           -100
blue            -100 (parent was added to scene before green great grandparent)
+4

Source: https://habr.com/ru/post/1623132/


All Articles