swift - SKEmitterNode 有问题吗?

标签 swift swift3 sprite-kit skemitternode

所以我的一个 SKEmitterNode 有问题。我有一个 SKSpriteNode,触摸它会打开一个新场景。这是开始的代码:

 for touch: AnyObject in touches {
        let location = (touch as! UITouch).location(in: self)
        if let nodeName = self.atPoint(location).name {

            if nodeName == "playBox" || nodeName == "playButton" {
                buttonSound()
                pulse(playBox, scene: "GameScene")
            }else if nodeName == "shopBox" || nodeName == "shopButton"{
                buttonSound()
                pulse(shopBox, scene: "shop")
            }}}

此时,一切正常。当我将我的 SKEmitterNode 添加到场景时,我的问题就出现了。发射器是一个星空效果,所以小点从场景的顶部到底部。一旦我添加了这个发射器,我的按钮就停止工作了!

我已经尝试了所有降低生成率、降低 zPosition 的方法,但似乎没有任何效果。

如果您有任何建议,请告诉我。谢谢!

-马特

最佳答案

很明显, HitTest 检测的是粒子系统,而不是按钮。您可以使用 nodes(at:) 来获取该点的所有节点列表(而不仅仅是第一个节点)。然后您需要迭代或过滤该数组并找出这些节点中哪些是按钮。

for touch: AnyObject in touches 
{
    let location = (touch as! UITouch).location(in: self)
    let nodes = self.nodes(at:location)

    let filtered1 = nodes.filter{ $0.name == "playBox" || $0.name == "playButton" }
    let filtered2 = nodes.filter{ $0.name == "shopBox" || $0.name == "shopButton" }

    if let node = filtered1.first {
        buttonSound()
        pulse(playBox, scene: "GameScene")
    }

    if let node = filtered2.first {
        buttonSound()
        pulse(shopBox, scene: "shop")
    }

关于swift - SKEmitterNode 有问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44703468/

相关文章:

swift - 从以前的 VC 转到 ViewController

ios - Swift 3 从数组中删除不存在于另一个数组中的对象并保持顺序

ios - SpriteKit-随机生成

ios - 无法将 SKNode 转换为 SKNode 的子类

swift - 使用 SwiftUI 时调用 "viewDidLoad"时如何运行函数

arrays - 如何获取数组/字典的切片并追加

swift - 如何在 Firebase Swift 中访问具有特定 UID 的用户的数据?

swift - 动画后如何去除SKTexure

ios - 将枚举案例与参数进行比较

swift - 在 SpriteKit Swift 中移动相机