ios - 如何检测 child Sprite 的触摸

标签 ios swift cocoa-touch parent-child

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

添加信息

这是我的两个 Sprite :

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

关于ios - 如何检测 child Sprite 的触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37264338/

相关文章:

swift - 具有类绑定(bind)约束的通用类不能由类绑定(bind)协议(protocol)参数化

ios - UIButtons 上的 UISwipeGestureRecognizer

iphone - 在实例变量中保存 block

html - 如何在 iOS 上获取最终的 HTML 字符串

ios - 在其他 View 前面添加来自 xib 的 View

ios - 为什么 Storyboard约束会自动更改?

json - Alamofire 超时在 Swift 3 中不起作用

ios - 如何在 Realm 中移动对象

iphone - CLLocationmanager : Save battery

ios - 当@synthesized 然后在子类中访问时,@properties 在调试器中的行为异常