我在我的视图中放置了多个SCNNode来加载我的应用程序。在touchesbegan上,我正在删除任何被点击的节点。
所有这些工作到目前为止所以我知道我的代码工作,但只是添加SCNParticleSystem给我问题。
我用不起作用的线放了两颗星(**)
// On tap
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
// Register tap
let touch = touches.first!
// Get location
let location = touch.location(in: sceneView)
// Create a hit
let hitList = sceneView.hitTest(location, options: nil)
if let hitObject = hitList.first {
// Get node from hit
let node = hitObject.node
if node.name == target {
score += 3
playAudio(fileName: "two")
**let explosion = SCNParticleSystem(named: "stars.scnp", inDirectory: nil)
**node.addParticleSystem(explosion!)
node.removeFromParentNode()
// Async call
DispatchQueue.main.async {
node.removeFromParentNode()
self.scoreLabel.text = String(self.score)
}
}
}
}
如何将粒子附加到节点?
如果要查看爆炸并删除节点,只需设置等待计时器,例如:
let explosion = SCNParticleSystem(named: "stars.scnp", inDirectory: nil)
node.addParticleSystem(explosion!)
let waitAction = SCNAction.wait(duration: 3)
node.runAction(waitAction, completionHandler: {
self.node.removeFromParentNode()
self.scoreLabel.text = String(self.score)
})
您可以在任何节点上发布等待操作,因此如果场景中有一个中心节点,它也可以使用它