【问题标题】:SKNode containsPoint is not workingSKNode containsPoint 不工作
【发布时间】:2023-03-15 15:25:01
【问题描述】:

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

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

    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
    }
}

【问题讨论】:

  • 寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现它所需的最短代码。跨度>

标签: ios swift compiler-errors sprite-kit


【解决方案1】:

使用locationInNode 代替locationInView

let 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)) {

        }
    }    
}

【讨论】:

  • 有没有办法说如果touchesEnded和touchesBegan在同一个位置,然后做点什么?
  • 这确实是一个新问题。对于同一根手指,返回的触摸对象始终相同,直到您在 touchesEnded 中将其从屏幕上抬起。您可以将它们与 === 运算符进行比较。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多