2

How can I get the nodes at a certain point in cocos2d? In SpriteKit I can do the following:

[parentNode nodesAtPoint:aPoint];

and it returns an array with all the nodes at that position. Is there something similar in Cocos2d (3)?

James Webster
  • 31,873
  • 11
  • 70
  • 114
Matthijn
  • 3,126
  • 9
  • 46
  • 69

1 Answers1

3

I don't think cocos2d has a method for this(correct me if I am wrong). The way I check for the node's which are at a certain point with a touch event is like this:

CGPoint location = [touch locationInView:[touch view]];
for (CCNode node in [parent children]) //if parent is the current scene/layer with the objects to check
{
        //if you want 1 type of class
        if (node isKindOfClass:[EnemyController class] ) { 
            if (CGRectContainsPoint(node.boundingBox, location)) {
                //your code here, for example:
                [yourArray addObject:node]; //if you want the objects in a predefined array
                break; //break if you only want the first object that comes along
            }
        }
}
  • Note that in order for that to work reliably, you'll need to convert the `location` to the node's parent's coordiante space by calling `convertToNodeSpace()` on the parent. – namezero May 30 '15 at 07:36