我知道以前可能有人问过这个问题,但没有什么能完全满足我的需要。现在想象一下这种情况,我有一个游戏,在这个游戏中,屏幕中间有一个名为 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/