我正在尝试创建一个切换场景的按钮。我知道用于切换场景的代码,但是我用于通过按钮执行操作的代码不起作用。

更新:我在第一个场景上使用了代码,但是当我在另一个场景上使用相同的代码(我切换了按钮和场景)时,它不起作用。
有人知道为什么吗?

    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    let gamestartScene = GameStartScene(size: self.frame.size)
    var location = CGPoint(x: 0, y: 0)
    var touch = touches.anyObject() as UITouch
    location = touch.locationInView(self.view)
    if menuButton.containsPoint(location){
   self.removeChildrenInArray([menuButton,replayButton,highScoreLabel,scoreLabela])
        self.view?.presentScene(gamestartScene)
    score = 0
    }
}

最佳答案

使用locationInNode而不是locationInViewlet location = touch.locationInNode(self)
UIView坐标的原点和SKScene坐标的原点不同。 UIView的原点(0,0)在左上方。 SKScene的起源在左下方。因此,函数locationInNodelocationInView将返回不同的结果。

override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
    for touch: AnyObject in touches {
        let location = touch.locationInNode(self)

        if(button.containsPoint(location)) {

        }
    }
}

10-01 17:27
查看更多