我正在尝试在场景中添加 SKShapeNode,但如果我使用它:
let rect = SKShapeNode(SKShapeNode(rect: CGRectMake(0, 0, 100, 100)))
self.addChild(rect)
该矩形添加在最后一层并与其他节点重叠。那么,如何在第一层添加呢?
提前致谢
最佳答案
您需要设置zPosition
您的 SKNode
的属性。来自文档:
The default value is 0.0. The positive z axis is projected toward the viewer so that nodes with larger z values are closer to the viewer. When a node tree is rendered, the height of each node (in absolute coordinates) is calculated and then all nodes in the tree are rendered from smallest z value to largest z value. If multiple nodes share the same z position, those nodes are sorted so that parent nodes are drawn before their children, and siblings are rendered in the order that they appear in their parent’s children array. Hit-testing is processed in the opposite order.
The SKView class’s ignoresSiblingOrder property controls whether node sorting is enabled for nodes at the same z position.
由于您似乎希望将 rect
节点置于其他节点下方,并且默认 zPosition
为 0.0
,因此尝试将其设置为 -1.0
:
let rect = SKShapeNode(SKShapeNode(rect: CGRectMake(0, 0, 100, 100)))
rect.zPosition = -1.0
self.addChild(rect)
关于swift - 如何在第一层SWIFT上添加SKNode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25671360/