如何检测 child 精灵的触摸

How to detect a touch of a child sprite

我知道以前可能有人问过这个问题,但没有什么是我需要的。现在想象一下这种情况,我有一个游戏,在这个游戏中,屏幕中间有一个名为 Background 的 SKSpriteNode。在那个 Background Sprite 上有一个名为 Button 的 child。我如何检测按钮上的触摸是否结束。如果有人可以帮助我编写 TouchesEnded 函数的代码,并可能更详细地解释正在发生的事情。谢谢,swift 会很理想,但我想如果需要的话我可以阅读 Objective-C。

添加信息

这是我的两个精灵:

let buttonBackground = SKSpriteNode(imageNamed: "Button Background.png") //The parent node of button
let soundOn = SKSpriteNode(imageNamed: "Sound On.png") //The button being pressed, which is a child of buttonBackground

这是我的 touchesEnded 函数:

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
    // Called when a touch begins

    for touch in touches {
        let location = touch.locationInNode(self)
        let nodeAtTouch = self.nodeAtPoint(location)
        let touchedSprite = nodeAtTouch as! SKSpriteNode

        if touchedSprite == button {
            print("YES")
        }
    }
}

当一次触摸结束时,从该点获取节点

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
    for touch in touches {
        let location = touch.locationInNode(self)
        let something = self.nodeAtPoint(location)
        let skNode = something as! SKSpriteNode
        //Do something with skNode
    }
}