gpt4 book ai didi

swift - zPosition 的 SpriteKit 问题

转载 作者:搜寻专家 更新时间:2023-10-31 08:23:02 25 4
gpt4 key购买 nike

我在 SpriteKit 场景中遇到了节点 zPosition 的问题。

我正在构建一个管理器节点,它支持对节点 zPosition 进行“类似层”的管理,本质上使您能够摆脱 zPosition 属性的手动管理,而是使用 addNodeUnder( node:SKNode) 以及创建组。

这是我制作的测试场景:
We can see a blue node, itself under a green node, itself under a yellow node, itself under a red node.

问题是根据我所做的操作,绿色和黄色节点应该在红色节点的顶部

我制作了自己的小调试器,按层次顺序显示 SceneKit“场景图”:

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.

如你所见:

  • 红色节点包含在 zPosition = 0
  • 的节点内
  • 蓝色节点包含在具有 zPosition = -100
  • 的节点内
  • 黄色和绿色节点包含在组节点内,组节点本身包含在 zPosition = 100
  • 的节点内

因为包含红色节点的节点有 zPosition = 0 而包含绿色和黄色节点的节点有 zPosition = 100,这两个节点不应该位于红色节点的之上

我的 SKViewignoresSiblingOrder 顺便设置为 false...

编辑:这很奇怪:yellowNode.zPosition = 1000 确实使黄色节点位于红色节点之上。怎么可能?

编辑 #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 框架方法来显示它,因此我的调试器的输出绝对正确。

最佳答案

来自 Apple 的文档,

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:

  1. Each node’s global z position is calculated.
  2. Nodes are drawn in order from smallest z value to largest z value.
  3. If two nodes share the same z value, ancestors are rendered first, and siblings are rendered in child order.

在哪里

node's global z position = node's z position + node's parent's z position + node's parent's parent's 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 节点(即它的曾祖父节点)被添加到蓝色的 LMWarpperNode 节点之后的 LMManagerNode 节点,因此首先绘制蓝色,然后在蓝色之上绘制绿色。

这是最终的(相反的)绘制顺序

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

关于swift - zPosition 的 SpriteKit 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34638828/

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