【问题标题】:SKSpriteNode touch detected swift快速检测到 SKSpriteNode 触摸
【发布时间】:2015-12-23 18:08:03
【问题描述】:

我无法检测到被触摸的特定节点。这是我必须要做的。

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 swift touch nodes skspritenode


    【解决方案1】:

    你没有使用最新的 Xcode 吗?你的 touches started 代码不应该在 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
            }
        }
    }
    

    【讨论】:

    • 您能否将问题标记为已回答。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多