swift - SCNParticleSystem 未在 touchesbegan 上添加到 SCNNode

标签 swift xcode arkit particle-system scnnode

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

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

关于swift - SCNParticleSystem 未在 touchesbegan 上添加到 SCNNode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55941203/

相关文章:

ios - 带有 rxswift 的多个 map

swift - 3D 模型看起来与 ARKit 中的大小不一样

ios - 如何确保 OBJ 模型以正确的方向显示?

ios11 - ARKit - ARSession 问题

swift - 如何在 MapKit 中制作长按事件

swift - 有没有一种组合方法可以阻止多个请求被触发?

swift - 在 swift 中导入框架不起作用 Xcode 6.3.1

swift - Mac 操作系统 : Remember Security & Privacy options for app while developing

ios - Git Commit 适用于 Xcode beta,但不适用于普通版本

ios - 如何快速显示事件指示器?