gpt4 book ai didi

swift - SKNode 相对于其父节点的 zPosition?

转载 作者:搜寻专家 更新时间:2023-10-30 21:52:07 30 4
gpt4 key购买 nike

我一直认为 SKNode 的 zPosition 是相对于其父节点的,但现在我遇到了相反的效果。

我的场景中有两个父 SKNode,它们的 zPosition 分别为 1 (node1) 和 2 (node2)。我想要实现的是 node2 应该始终在 node1 之上分层。但不幸的是,node1 的子节点(其 zPosition 为 1-50)都位于 node2 的子节点(当前没有 zPosition)之上。有没有办法解决这个问题(除了给node2一个超过50的zPosition)?也许是某种 bool 参数来设置所有子节点相对于其父节点的 zPosition?任何帮助将不胜感激!

编辑:为了更清楚,这里是所有元素的层次结构:

  • node1:SKNode (zPosition 1)
    • child1:SKNode (zPosition 1)
    • child2:SKNode (zPosition 2)
    • child3:SKNode (zPosition 3)
    • ...
    • child50:SKNode(zPosition 50)
  • node2:SKNode (zPosition 2)
    • child1:SKLabelNode(无 zPosition)
    • child2:SKLabelNode(无 zPosition)
    • child3:SKLabelNode(无 zPosition)
    • ...
    • child50:SKLabelNode(无 zPosition)

EDIT2:这是我的 GameScene 类的简化版本:

import SpriteKit

class GameScene: SKScene {

let gameNode = SKNode()
let node1 = SKNode()
let node2 = SKNode()

let nodeSize:CGFloat = 50.0
let spacing:CGFloat = 5.0

required init(coder aDecoder: NSCoder) {
fatalError("NSCoder not supported")
}

override init(size: CGSize) {
super.init(size: size)

self.addChild(gameNode)

node1.name = "node1"
node2.name = "node2"

node1.zPosition = 1
node2.zPosition = 2

gameNode.addChild(node1)
gameNode.addChild(node2)

// add children to node1
for i in 1..<10 {
let child = SKSpriteNode(color: UIColor.redColor(), size: CGSize(width: nodeSize, height: nodeSize))
child.position = CGPointMake((nodeSize + spacing) * CGFloat(i), 0)
child.zPosition = CGFloat(i)
self.node1.addChild(child)
}

// add children to node2
for i in 1..<10 {
let child = SKLabelNode(fontNamed: "Avenir Black")
child.position = CGPoint(x: (nodeSize + spacing) * CGFloat(i), y: 0)
child.text = "label\(i)"
child.fontSize = 10
child.fontColor = UIColor.blackColor()
self.node2.addChild(child)
}
}
}

运行它时,您可以看到只有 node2 的第一个子节点(label1)位于 node1 的第一个子节点之上(因为它们具有相同的 zPosition)。所有其他节点都位于 node1 的子节点之下。

最佳答案

阅读 Apple Documentation 时关于这一点,您可以找到:

Maintaining the order of a node’s children can be a lot of work. Instead, you can give each node an explicit height in the scene. You do this by setting a node’s zPosition property. The z position is the node’s height relative to its parent node, much as a node’s position property represents its x and y position relative to parent’s position. So you use the z position to place a node above or below the parent’s position.

When you take z positions into account, here is how the node tree isrendered:

  • 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.

我发现的一点 - 我不知道它是这样的 - 是 child zPosition 是计算他们 parent zPosition 的总和,所以如果 node1.zPosition + (node1's)child3.zPosition = 4, and node2.zPosition + (node2's)child3.zPosition = 3, node1 的 child 会被画在上面。

解决方案:设置 node2.zPosition 大于 (node1's)lastChild.zPosition + node1.zPosition

关于swift - SKNode 相对于其父节点的 zPosition?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31557879/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com