0

I instance a sprite, and then when it collides with a second sprite, the child of that sprite is removed:

if (CGRectIntersectsRect(spriteOne.boundingBox, self.swat.boundingBox)) 
     {
         if (spriteOne.tag == 0){
             [self removeChild:spriteOne cleanup:YES];
         }
         if (spriteOne.tag == 1){
             [self removeChild:spriteOne cleanup:YES];
         }
}

This works how I want it to, and the sprite disappears off the screen. However, it seems that the boundingBox is still there, even if the image is not, and this causes trouble with scoring, etc... So, what I would like to know is how to 'dis-activate' the sprite's boundingBox so that when the first time that the two sprites colide, the collision is detected, but any time after that, it is not.

Thanks in advance.

akuritsu
  • 440
  • 1
  • 4
  • 18
  • how does "the bounding box is still there" manifest itself ? what error are you observing ? – YvesLeBorg Jul 01 '12 at 15:44
  • well when the two sprites collide, I call a method, which adds +1 to a score. What happens is that the first time that they collide, there is +1 added to the score, and the first sprite disappears. However, anytime that the second sprite hovers over the where the first sprite 'died', additional points are added. – akuritsu Jul 02 '12 at 10:02

2 Answers2

0

As far as I understand, you should have some issue with removing the sprite after a collision.

Would you try this?

if (CGRectIntersectsRect(spriteOne.boundingBox, self.swat.boundingBox)) 
 {
     if (spriteOne.tag == 0){
         [spriteOne removeFromParentAndleanup:YES];
     }
     if (spriteOne.tag == 1){
         [spriteOne removeFromParentAndleanup:YES];
     }
 }

Have you tried adding some NSLog traces to see whether the sprite is really removed?

sergio
  • 68,819
  • 11
  • 102
  • 123
0

You must be retaining spriteOne. If there is a good reason to keep it around, do this:

if ( spriteOne.visible && CGRectIntersectsRect(spriteOne.boundingBox, self.swat.boundingBox)) 
 {
     if (spriteOne.tag == 0){
         spriteOne.visible=NO;
     }
     if (spriteOne.visible && spriteOne.tag == 1){
         spriteOne.visible=NO;
     }
}

Later, when you need spriteOne in play again, just set its visibility to YES;

If not, you have a leak, and do this:

if ( spriteOne && CGRectIntersectsRect(spriteOne.boundingBox, self.swat.boundingBox)) 
 {
     if (spriteOne.tag == 0){
         [self removeChild:spriteOne cleanup:YES];
         self.spriteOne=nil;    // assumes you have a property for spriteOne
     }
     if (spriteOne && spriteOne.tag == 1){
         [self removeChild:spriteOne cleanup:YES];
         [spriteOne release];   // assumes no property for spriteOne
         spriteOne=nil;         // dont forget this ! beware of zombies
     }
}
YvesLeBorg
  • 9,070
  • 8
  • 35
  • 48