【发布时间】:2021-12-12 14:36:48
【问题描述】:
在我的一个 SpriteKit 游戏中尝试实现粒子轨迹效果时,我遇到了一些奇怪的事情:将SKEmitterNode 的targetNode 设置为self.scene 时,粒子不再可见。
实际情况是这样的:粒子正在渲染a)前面,或b)后面背景图像SKSpriteNode,具体取决于是否或未设置targetNode。您可以通过给背景节点一些 alpha 透明度来亲自看到这一点,这样您就可以透过背景看到粒子。
在发射器节点及其父节点上设置相关的zPosition并不能解决问题——无论zPosition如何,粒子都会渲染在背景节点后面(假设设置了背景节点的zPosition)。
我已将其精简为一个可重现的最小示例;如果你将这个 GameScene 插入到一个新的 SpriteKit 项目中,你应该会看到我所说的行为。
对于背景图片,只需使用任何不透明的图片。对于粒子文件,只需使用标准的“Smoke”模板即可。
注意:粒子在模拟器上正确渲染,所以请使用真实设备。
class GameScene: SKScene {
override func didMove(to view: SKView) {
let screen = UIScreen.main.bounds
self.scene?.backgroundColor = .white
self.scaleMode = .resizeFill
self.anchorPoint = CGPoint(x: 0.0, y: 0.0)
let background = SKSpriteNode(imageNamed: "testBackground")
let parent = SKSpriteNode()
let emitter = SKEmitterNode(fileNamed: "SmokeParticles")
background.size = CGSize(width: screen.width, height: screen.height)
background.position = CGPoint(x: screen.width*0.5, y: screen.height*0.5)
background.zPosition = 0.5 //Giving the background a zPosition is necessary to reproduce the problem.
//background.alpha = 0.7 //Add this line to see that the particles are BEHIND the background when 'targetNode' is set.
parent.size = CGSize(width: 50.0, height: 50.0)
parent.position = background.position
parent.zPosition = 5.0
if let emitter = emitter {
emitter.zPosition = 5.0 //I would think this should solve the problem, but it doesn't
emitter.targetNode = self.scene //Comment or uncomment this line to toggle the problem on/off.
parent.addChild(emitter)
}
addChild(background)
addChild(parent)
}
func touchDown(atPoint pos: CGPoint) {
}
func touchMoved(toPoint pos : CGPoint) {
}
func touchUp(atPoint pos : CGPoint) {
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for t in touches { self.touchDown(atPoint: t.location(in: self)) }
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for t in touches { self.touchMoved(toPoint: t.location(in: self)) }
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
for t in touches { self.touchUp(atPoint: t.location(in: self)) }
}
override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
for t in touches { self.touchUp(atPoint: t.location(in: self)) }
}
override func update(_ currentTime: TimeInterval) {
}
}
问题:这是一个错误,还是某个被遗弃的功能?您是否看到同样的行为?
【问题讨论】:
-
将 targetNode 分配给 SKScene 有什么意义?必须是场景的 SKNode 子节点
-
Apple 的文档建议将
targetNode分配给SKScene此处 -> developer.apple.com/documentation/spritekit/skemitternode/… ... 和此处 -> developer.apple.com/documentation/spritekit/skemitternode/…
标签: ios swift sprite-kit