【发布时间】:2023-03-13 11:15:01
【问题描述】:
我在 SpriteKit 场景中遇到节点的 zPosition 问题。
我正在构建一个管理器节点,它可以对节点 zPosition 进行“层状”管理,本质上使您能够摆脱 zPosition 属性的手动管理,而是使用 addNodeUnder(node:SKNode) 等方法添加节点以及创建组。
问题是通过我所做的操作,绿色和黄色节点应该在红色节点的顶部。
我制作了自己的小调试器,以分层顺序显示 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,那么这两个节点不应该在红色节点的顶部 ?
顺便说一句,我的SKView 的ignoresSiblingOrder 设置为false...
编辑:这很奇怪:yellowNode.zPosition = 1000 确实使黄色节点位于红色节点之上。怎么可能?
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 框架方法来显示它,所以我的调试器的输出肯定是正确的。
【问题讨论】:
-
我相信事情发生了变化,并且父节点内的节点不再以某种方式尊重 zPosition,您是否尝试设置黄色和绿色节点 zPosition 而不是父母 zPosition,看看是否名列前茅?
-
这是有线的:
yellowNode.zPosition = 1000确实使黄色节点位于红色节点之上。怎么可能? -
回答?您的意思是评论,我目前正在为您输入答案
-
我建议你阅读标题为Understanding the Drawing Order for a Node Tree的部分
-
@TrevörAnneDenise 是的,我明白,我的意思是允许您使用常规 SKNode 作为容器节点来模拟问题,而 SKSpriteNode 用于精灵而不是使用真实代码...
标签: swift sprite-kit