1

I am trying to detect collisions in cocos2D. I am using this code:

- (void)checkForCollisionSpeedUp:(ccTime)dt

{

    CGRect projectileRect = CGRectMake(
                                       guy.position.x, 
                                       guy.position.y, 
                                       2, 
                                       20);


        CGRect targetRect = CGRectMake(
                                       speedUp.position.x - (speedUp.contentSize.width/2), 
                                       speedUp.position.y - (speedUp.contentSize.height/2), 
                                       speedUp.contentSize.width, 
                                       speedUp.contentSize.height);
        if (CGRectIntersectsRect(projectileRect, targetRect)) {
            [[SimpleAudioEngine sharedEngine] playEffect:@"Robot_blip-Marianne_Gagnon-120342607.wav"];
            [bg removeChild:speedUp cleanup:YES];

    }
}

That code detects the collision, it plays the sound, and removes the sprite, but not the CGRect. The CGRect remains in the position of the sprite when it was deleted. How do I solve this?

Thanks,

Tate

Also, I really don't want to use Box2D or Chipmunk for collision detection.

2
  • What do you mean that CGRect remains in the position? Commented Feb 5, 2011 at 16:26
  • @arul So basically, my app has sprites falling down at you. When they collide with you, the sound will play and like I said, the sprite will be removed. The problem is, if you move to the spot where the sprite was deleted, it still detects collisions and plays the sound over and over again, almost as if the sprite was never deleted, which is why I don't think that that the CGRect was ever deleted. Hope that makes sense. Commented Feb 5, 2011 at 16:44

1 Answer 1

1

Your problem description indicates that the sprite was never removed from memory. If you created it with alloc/init you probably forgot to release it. If you created it from autorelease initializer, you probably retained it.

Also, use the [self boundingBox] method to get a sprite's bounding box. It's faster and more flexible.

Sign up to request clarification or add additional context in comments.

1 Comment

My sprite was created like this: CCSprite *sprite = [CCSprite spriteWithFile:@"file.png"]; Then I added it as a child. How do I get rid of it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.