swift - 如何在第一层SWIFT上添加SKNode

标签 swift sprite-kit sknode

我正在尝试在场景中添加 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 节点置于其他节点下方,并且默认 zPosition0.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/

相关文章:

arrays - 将 unicode 代码点数组转换为字符串

swift - 将 UIView 置于状态栏上方

objective-c - 从 UIViewController 打开 SKScene 时出现 NSInvalidArgumentException

swift - 如何在给定时间内移动节点?

ios - Swift 中的类扩展与子类化?

ios - firebase 数据库结构的好方法,以便按日期排序?

ios - 通过名称访问 Sprite 节点

swift - 在 Swift SpriteKit 中禁用节点和 FPS 标签

Swift-在一个角度上简单地制作一个 SKNode "move forward"

swift - 移动时如何访问 SKSpriteNode 子类的对象(Swift)