- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我在 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 确实使黄色节点位于红色节点之上。怎么可能?
编辑 #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:
- 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.
在哪里
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/
我正在尝试构建一个 SpriteKit 游戏,但它因错误而失败:“无法将模块“SpriteKit”加载为“Spritekit”” 我使用的是 Xcode 版本 6.3.2 (6D2105)。 最佳答案
如何在 spritekit swift 中更改碰撞时的 spritekit。 else if collidedBird.birdType == 6 { if U
我刚刚创建了一个新的 SpriteKit 项目,就遇到了砰!问题。我运行该项目时没有触及任何内容,并且应该如此,默认的 hello world 项目出现了。这方面一切都很好。现在,我创建了一个新的 C
当我使用 SpriteKit 创建视频对象时 let background = SKVideoNode(videoFileNamed: "Video.mov") 我只使用了一次命令: backgrou
出于某种原因,我无法将节点放置在屏幕的顶部或底部。我曾经能够使用 self.frame.height/2 很好地做到这一点,但现在我在景观中工作,这行代码不起作用。注意:我的 anchor 是 0.5
在场景中“缩小”的正确方法是什么。 我有一个物体,我施加一个冲动将它发射到屏幕上。例如,它会发射大约 100 像素,这按预期工作 - 增加它飞行的力量,增加它飞行的密度更少等等。 我遇到的问题是缩放,
是否可以更改粒子发射的频率。例如,如果我发射一个粒子,我可以让它每 5 秒或 10 秒发射一次吗? 我搜索了文档,但找不到任何内容。有解决方法吗?我想做这样的事情: emitter.partic
有谁知道一些开箱即用的库,它们可能会为 SKNode 提供诸如相机抖动之类的效果。 ? 如果没有,是否有一种简单的方法可以使用 Action 来实现相机抖动? 谢谢 最佳答案 我找到了一种使用 SKA
我目前一直在使用委托(delegate)和 SKView 自定义类来监视我的 OS X SpriteKit 游戏中的按键。我需要为我的键盘监控类雇用多个代表,我知道这是不可能的,但可以通过使用观察者来
我从事 SpriteKit 游戏已经有一段时间了,但我有一个无法摆脱的恼人问题。我不想过多介绍这款游戏,但它非常简单。我每秒生成一些对象(SKSpriteNodes),这些对象使用 SpriteKit
我正在制作基于SpriteKit的多级游戏。 一切正常,除了一件事:当用户玩很长时间、改变很多关卡等时......然后 SpriteKit 开始丢失纹理。 我的意思是,没有像图像加载失败时那样的大红叉
我正在尝试将 SKSprite Particle Emitter 与 Swift 一起使用。 但我想在我的发射器中使用许多不同的纹理。 是否有可能:拥有许多图像,并且让发射器随机使用图像,而不是仅使用
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 这个问题似乎偏离主题,因为它缺乏足够的信息来诊断问题。 更详细地描述您的问题或 include a mini
我正在用SpriteKit构建我的第一个项目,但我无法弄清楚这部分。 我的项目包括飞入屏幕的敌方直升机。每个敌人播放大约14秒长的声音文件。这不是循环。它包括敌人进入屏幕之前接近的声音,以及在屏幕上移
我试图在代码中到处搜索:解释文档在进入后台时会做什么,或者甚至有时会暂停,但无济于事-有人可以指导我在进入后台时建议做什么到启用 Sprite 套件的游戏中的背景吗? 我应该调用scene.pause
我是 SpriteKit 新手。我需要通过 UIImageView 或 SpriteNode 显示图像(它是游戏的背景图像)。但是,我需要用户能够放大背景并平移。我在不使用 SpriteKit 的情况
在我的游戏中,我正在创建一些受重力影响的球。当游戏开始时,他们会从屏幕外掉下来,并在底部找到自己的位置。没有其他力施加在他们身上。 问题是我正在用精确的坐标、精确的物理属性创建这些球,但结果不一样。它
我想让我的宇宙飞船有一条跟随它的粒子轨迹。我该怎么做? 这是我的宇宙飞船的代码: override func didMove(to view: SKView) { spaceS.positio
我正在尝试制作一个动画角色,可以在不移动他的躯干的情况下伸手去拿屏幕上的东西。 如果他够不到,他的手应该朝这个方向移动最大量,然后被他的静态躯干的物理限制所抵消。 所以在我的代码中,绿色矩形( han
我的场景上有player对象,并试图对其施加一些力。您可以看到我正在对update:文件上的MyScene.h方法施加作用力。但是,我的player根本没有动。发生了什么? 下面是Player SKS
我是一名优秀的程序员,十分优秀!