0

I have a SKShapeNode and i set a name for it, but when i try to detect it in touch began it doesn't work the way you use with other sprites.

 override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
        /* Called when a touch begins */

        for touch: AnyObject in touches {
            let location = touch.locationInNode(self)



            if let body = self.nodeAtPoint(location) as? SKSpriteNode {

                if var name: String = body.name {
                    switch name {

                        case "myShape":
                             println("Shape Touched")

                        case "enemy":
                             println("Enemy Touched")

                    default:


                    }
                }
            }
        }
    }

enemy is a SKSpriteNode and it gets detected correctly, but shape which is SKShapeNode doesn't. And i typed the string name correctly.

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
Abdou023
  • 1,654
  • 2
  • 24
  • 45

1 Answers1

0

When detecting touches with SpriteKit, I've found that it's easier to use SKNode's contiansPoint() method rather than nodeAtPoint()

let myShape = self.childNodeWithName("myShape")
if myShape.containtsPoint(location) {
    println("Shape touched")
}

Obviously, my code example won't scale very well at all, but in general containsPoint() has been much more reliable than nodeAtPoint() in my experience.

JSquared
  • 179
  • 3
  • 4
  • I changed my shape to a spriteNode and it works fine, looks like shapes must be filled with textures or something in order to get detected. – Abdou023 Feb 22 '15 at 05:37