touch.locationInView 没有像我想的那样工作

touch.locationInView not working as I thought

我正在努力做到这一点,以便在我触摸场景中的节点时我的游戏能够识别,除非我使用下面的代码,我认为这完全符合我的要求,我的应用程序无法正确识别节点的位置节点。

我在屏幕的其余部分周围单击,打印出我正在触摸的节点的名称,结果我的应用程序认为节点的位置与实际位置不同(x 值是正确的y 值非常偏离)

你看到什么地方不对了吗?

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    /* Called when a touch begins */

    for touch in (touches as! Set<UITouch>) {
        var TouchlocationEnd = touch.locationInView(self.view!)
        let touchedNode = self.nodeAtPoint(TouchlocationEnd)

        var name = touchedNode.name

        println(name)

        if name == "start"{
            var scene = PlayScene(size: self.view!.bounds.size)
            scene.scaleMode = .AspectFill
            self.view!.presentScene(scene)
        }
    }
}

根据 Apple 的文档,nodeAtPoint() 函数需要 "a point in the node’s coordinate system"。但是您找到的点是视图中的位置,它不一定与您在此处子类化的任何节点位于同一坐标系中。

也就是说,我猜测"self.view"的坐标系与"self"的坐标系不同。尝试这样的事情(未经测试):

let touchPoint = touch.locationInNode( self )
let touchedNode = self.nodeAtPoint( touchPoint )