- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我一直认为 SKNode 的 zPosition 是相对于其父节点的,但现在我遇到了相反的效果。
我的场景中有两个父 SKNode,它们的 zPosition 分别为 1 (node1) 和 2 (node2)。我想要实现的是 node2 应该始终在 node1 之上分层。但不幸的是,node1 的子节点(其 zPosition 为 1-50)都位于 node2 的子节点(当前没有 zPosition)之上。有没有办法解决这个问题(除了给node2一个超过50的zPosition)?也许是某种 bool 参数来设置所有子节点相对于其父节点的 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/
我发现如果你想在 Sprite Kit 中使用物理,你必须在世界坐标中指定 anchor 。假设您有一个大节点树,这里有一辆汽车,那里有一个轮子。您只知道它们在父节点中的坐标。您想要在汽车和车轮之间定
我正在为场景创建一个 SKNode,并在每个单独的 SKNode 上记录触摸事件。我可以根据需要添加任意数量的内容,如果我触摸节点并且只有可见的节点,我才能看到日志消息,并且触摸会按预期工作。现在,如
基本上我想实现这样的目标:https://github.com/moqod/iOS-Scratch-n-See在 SpriteKit 中。 在 SKScene 中,我有一张汽车图像,2-3 种不同类型
我从一个函数返回一个 SKNode,我需要将它转换为自定义 SKNode。我收到错误消息无法将 SKNode的值分配给类型GroundNode。如果我强制转换,它会编译,但在运行时失败。我错过了什么?
我正在尝试使用 SpriteKit 模拟一只眼睛。 当用户的手指在屏幕上移动时,眼睛的瞳孔会跟踪用户的手指,但必须保持在眼睛的范围内。 我曾尝试使用 SKConstraint 解决此问题,但未成功。
我不明白为什么我在最后一行收到这个错误:'SKNode!'不是'SKNode'的子类型 func move_background(){ let enumerationBlock: (SKNod
我有一个场景,其中有许多子节点,它们是 SKNode 的子类。我正在尝试访问每个子节点的方法,但收到错误“‘SKNode’类型的值没有成员‘[成员名称]”。 全部基于 Xcode 10.2 上的 Sw
我正在使用 Sprite Kit 创建一个 iOS 应用程序,并且我正在尝试选择 (onTouchBegan) 位于另一个 SKNode 下方的 SKNode。结构是:第一层(SKNode)-第二层(
我在带有 SKScene 的 UIView 上有一个 UITapGestureRecognizer 和一个 UIPanGestureRecognizer。平移手势识别器从左到右移动一个 SKNode,
当事件发生时,SKNode 子类应该如何通知父场景? 我有一个 SKNode 子类,它根据游戏逻辑通过适本地重绘或删除节点来处理触摸事件。通知父场景(或其他同级 SKNode 实例)事件发生的 iOS
我有正确的字符串格式的 SKNODE。我想将字符串名称“A2Square”转换为名为 A2Square 的节点。 这可能吗? 是否有办法将字符串“A2Square”并以某种方式将其转换为同名的 SKN
我已经设置了场景,一切都运行良好。它看起来像这样: 但是 Controller 是在 Main.storyboard 中设置的,我想摆脱它。所以我做了这样的事情: window = UIWin
我知道为什么会出现该错误,但我无法找到解决方法。我试图让对象出现然后被删除,玩家应该在它们被删除之前尝试点击它们,但每次下一个节点即将出现时它都会崩溃。如果我在它的 func 中声明它,那么一切都会出
我的场景是,我正在沿着拱形移动现有节点,并希望它在到达另一侧时停止。一个简单的比喻是,我想让一些东西沿着彩虹移动。 虽然我当前的代码确实执行了正确的椭圆形,但它随后在我的 y 原点下方继续返回到其起点
我正在获取 int SpriteKit。并且想知道如何在 SKNode 对象上创建运动效果。 对于 UIView,我使用以下方法: +(void)registerEffectForView:(UIVi
我在不同的文件中有两个类。 在我的场景所在的“主”文件中,我调用此函数: override func touchesBegan(touches: Set, withEvent event: UIEve
好的,所以我有代码会在 1 和 2 之间选择一个随机数,并基于该数生成一个 metalCrate: 或只是一个 crate :。我想,金属 crate 应该需要轻按 2 次才能消失。不幸的是,使用我的
我目前正在同时使用 ARKit 和 CoreLocation 将元素放置在屏幕上。这些元素(例如建筑物、房屋等)基于它们自己的坐标(经度/纬度)和用户坐标放置。如您所知,即使设置了最佳精度,CoreL
我创建了一个运行良好的 SKNode 类。但是,我无法在实例化后更改其 .position。 按钮确实出现并正常工作,但在编程时不会改变位置。我已经包含了为下面的按钮制作的类,我还添加了实例化该类并调
出于学习目的,我正在使用 SpriteKit 和 Swift 5 编写一个类似 Pong 的游戏。我试图让玩家的 Racket (一个 SKNode 矩形)在 x 坐标的屏幕上移动但不能。 我试过这个
我是一名优秀的程序员,十分优秀!