xcode - SKSpriteNode 快速检测到触摸

标签 xcode swift touch nodes skspritenode

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

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/

相关文章:

swift - AVAudioPlayer 即将为零

ios - UITableView 单元格的动态高度问题 (Swift)

ios - 在 Swift 中的 View Controller 之间传递数据

ios - 使用 anchor 的自动布局没有按照我的预期方式布置我的 View ?

javascript - Windows 8 html5 javascript 应用程序中向左滑动的事件名称是什么?

android - android 使用软键盘打字时的 touchEvent

android - 同一语言的两个版本的应用程序本地化

ios - UIImageView动画开始并出现内存不足警告

swift - Firebase/Firestore - 完成 block 未按预期工作

Java Swing - 触摸屏 - L & F