【问题标题】:SCNParticleSystem not adding to SCNNode on touchesbeganSCNParticleSystem 未在 touchesbegan 上添加到 SCNNode
【发布时间】:2019-09-20 07:29:03
【问题描述】:

我在加载我的应用程序时在我的视图中放置了多个 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)
            }
        }
    }
}

如何将粒子附加到节点?

【问题讨论】:

  • 您正在立即移除节点,因此不会看到粒子系统的任何效果
  • @TheInterloper 你是对的,我删除了删除节点的行,我的爆炸成功了!你知道我怎样才能让两者都发生吗?
  • 在下面作为答案发布

标签: swift xcode arkit particle-system scnnode


【解决方案1】:

如果你想看到爆炸并移除节点,只需设置一个等待计时器,例如:

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)
})

您可以在任何节点上发布等待操作,因此如果您在场景中有一个中心节点,它也可以使用它

【讨论】:

    猜你喜欢
    • 2018-03-28
    • 2019-08-11
    • 1970-01-01
    • 2018-08-10
    • 2019-01-29
    • 1970-01-01
    • 2019-06-29
    • 2019-09-06
    • 2020-06-24
    相关资源
    最近更新 更多