检测到 SKSpriteNode 触摸 swift

SKSpriteNode touch detected swift

我在检测被触摸的特定节点时遇到问题。这是我必须做的。

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吗?你的触摸开始代码不应该 运行 with swift 2 and 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
        }
    }
}