Search code examples
coordinatessprite-kitskshapenode

SpriteKit - How do I check if a certain set of coordinates are inside an SKShapeNode?


In my game, I'm trying to determine what points to dole out depending on where an arrow hits a target. I've got the physics and collisions worked out and I've decided to draw several nested circular SKShapeNodes to represent the different rings of the target.

I'm just having issues working out the logic involved in checking if the contact point coordinates are in one of the circle nodes...

Is it even possible?


Solution

  • The easiest solution specific to Sprite Kit is to use the SKPhysicsWorld method bodyAtPoint:, assuming all of the SKShapeNode also have an appropriate SKPhysicsBody.

    For example:

    SKPhysicsBody* body = [self.scene.physicsWorld bodyAtPoint:CGPointMake(100, 200)];
    if (body != nil)
    {
        // your cat content here ...
    }
    

    If there could be overlapping bodies at the same point you can enumerate them with enumerateBodiesAtPoint:usingBlock: