我在检测被触摸的特定节点时遇到问题。这是我必须做的。
let playagain = SKSpriteNode(imageNamed: "PlayAgain.png")
override func didMoveToView(view: SKView) {
super.didMoveToView(view)
}
然后当玩家死亡时这两个节点出现。
playagain.position = CGPoint(x:frame.size.width * 0.5, y: frame.size.height * 0.5)
addChild(playagain)
gameover.position = CGPoint(x:frame.size.width * 0.5, y: frame.size.height * 0.75)
addChild(gameover)
以上一切正常。该节点出现在屏幕上,我问我似乎无法让它显示我点击了它。 如您所见,该节点称为 playagain,当单击 playagain 节点时,我希望能够刷新游戏。我目前所拥有的如下。
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
for touch in touches {
let location = (touch as! UITouch).locationInNode(self)
let play = self.nodeAtPoint(location)
if play.name == "playagain" {
println("touched")
}
}
}
谢谢!
最佳答案
您使用的不是最新的 Xcode 吗?您的 touches 开始代码不应使用 swift 2 和 Xcode 7 运行。
试试这个
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
let location = touch.locationInNode(self)
if playagain.containsPoint(location) {
/// playagain was pressed, do something
}
}
}
或者这个
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
let location = touch.locationInNode(self)
let touchedNode = self.nodeAtPoint(location)
if touchedNode == playagain {
/// playagain was pressed, do something
}
}
}
关于xcode - SKSpriteNode 快速检测到触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34441381/