在我的一款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) {
}
}
问题:这是一个bug,还是一些该死的功能?你看到了同样的行为吗?
我最终通过使用SKEmitterNode
的particleZPosition
属性来解决这个问题。