Search code examples
sprite-kitcgpoint

SpriteKit Select Sprite node from CGPoint


First post,

I'm in Apple's SpriteKit framework...(new programmer) and I have a bunch of nodes that are stationary. How do I select/grab a particular node if I know the coordinates (CGPoints)? I basically want to say, grab that node at (x,y) and do something with it (i.e. change color)

Thanks


Solution

  • If you know the CGPoint, you use the command nodeAtPoint: However, this command only returns the deepest descendant that intersects a point. If you have the possibility of having more than one node at your CGPoint, you should use the command nodesAtPoint: instead.

    Read up on the details in the SKNode Class Reference - section titled Determining If a Point Lies in a Node.

    ** Update **

    CGPoint myPoint = CGPointMake(10, 10);
    SKNode *myNode = [self nodeAtPoint:myPoint];
    

    Where self either is or needs to be replaced with the parent node of the node you are looking for.