touch.locationInView not working as I thought

649 Views Asked by At

Im trying to make it so my game recognizes when I touch a node in my scene, except when I use the code below, which I thought would do exactly what I wanted, my app does not correctly identify the position of the node.

I clicked around the rest of the screen printing out the name of the node I was touching, and it turns out that my app thinks the nodes are in a different location than they really are (the x value is correct by the y value is very off)

do you see anything that is wrong?

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

There are 1 best solutions below

2
On BEST ANSWER

The nodeAtPoint() function, according to Apple's docs, takes "a point in the node’s coordinate system". But the point you found is the location in the view, which isn't necessarily in the same coordinate system as whatever node you're subclassing here.

In other words, I'm guessing that the coordinate system of "self.view" is different from the coordinate system of "self". Try something like this (untested):

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