Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Is there a method in Cocos2d like CGRectIntersectsRect, except instead of limiting it to one sprite, it checks for ALL objects?

share|improve this question
add comment

1 Answer

What you can do is get all the children in the current layer and then iterate through then and use the CGRectIntersectsRect function to check if the any node is present at your desired location.

After that you can do what ever you want to do with the nodes that are present at that frame.

You need to get all the nodes like this

CCArray *childrenNodes = [self children];

and check for intersection like this

for(CCNode *someNode in childrenNodes)
{
    CGRect rect = ((CCSprite *)someNode).textureRect;
    if(CGRectIntersectsRect(rect, desiredRect))
    {
        // --------------------->Do something
    }
}

Edit:
As mentioned in your below comment you can modify the above code to only check for a particular type of sprite or sprites. For this to happen you need to subclass ccsprite or give a tag to a set of sprite representing a particular type of object, then in the for loop only check for that sprite or sprites depending on your requirement.

for(CCNode *someNode in childrenNodes)
{
    if([someNode isKindOfClass:YourSpriteClass] || someNode.tag == 81)
        continue;
    CGRect rect = ((CCSprite *)someNode).textureRect;
    if(CGRectIntersectsRect(rect, desiredRect))
    {
        // --------------------->Do something
    }
}

I think you will get the idea from the above code.

share|improve this answer
    
But what about the other children in the room? What if I just wanted it to be for one type of object, like a wall, but not, say, a tree? –  Someone Feb 20 '12 at 16:41
    
hey @JackStone Did you try out the code with the changes that i made in my answer. –  Robin Feb 27 '12 at 8:30
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.