1
votes

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.

1
What do you mean that CGRect remains in the position? - arul
@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. - tallen11

1 Answers

1
votes

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.