0
votes

I'm using bounding boxes in cocos2d to detect collision between a ball and another sprite, but when I use [self removeChild:Row4Tile1 cleanup: YES]; the CGRect stays there, while the sprite is removed. Is there a way to delete the bounding box as well? I initialize each one as

Row4Tile1 = [CCSprite spriteWithFile:@"Row4.png" rect:(CGRectMake(Row4Tile1.position.x, Row4Tile1.position.y, 26, 73))];
Row4Tile1.color = ccc3(0, 168, 255);
Row4Tile1.position = ccp(301, 443);
Row4Tile1.rotation = 15.0;
[row4 addObject:Row4Tile1];
[self addChild:Row4Tile1];

And then I remove them after the if (CGRectIntersectsRect(ball.boundingBox, Row4Tile1.boundingBox)) I do however use the same image for multiple sprites so i'm not sure if this matters or not. Here is the code for the tick method:

 - (void)tick:(ccTime) dt {    
 _world->Step(dt, 10, 10);
    for(b2Body *b = _world->GetBodyList(); b; b=b->GetNext()) {
    if (b->GetUserData() != NULL) {
        ballData = (__bridge CCSprite *)b->GetUserData();
        ballData.position = ccp(b->GetPosition().x * PTM_RATIO,
                                b->GetPosition().y * PTM_RATIO);
        ballData.rotation = -1 * CC_RADIANS_TO_DEGREES(b->GetAngle());
         NSLog(@"ball position is %@", NSStringFromCGPoint(ballData.position));
        for(CCSprite *sprite in row4){
            if (CGRectIntersectsRect(ball.boundingBox, Row4Tile1.boundingBox)) {
                [self removeBall];
                [row4 removeObject:Row4Tile1];
                [self removeChild:Row4Tile1 cleanup: YES];
            }
            if (CGRectIntersectsRect(ball.boundingBox, Row4Tile2.boundingBox)) {
                [self removeBall];
                [row4 removeObject:Row4Tile2];
                [self removeChild:Row4Tile2 cleanup: YES];
            }
            if (CGRectIntersectsRect(ball.boundingBox, Row4Tile3.boundingBox)) {
                [self removeBall];
                [row4 removeObject:Row4Tile3];
                [self removeChild:Row4Tile3 cleanup: YES];
            }
   }

The remove ball method isn't there but that works fine so I didn't add it. It's just the row bounding boxes that aren't being removed

1
Are you using box2d debug mode display ? does your sprite have a body defined in the box2d world ? - giorashc

1 Answers

0
votes

« Talking about not removed memory? then here is one other similar problem.

You added Row4Tile1 to row4 array and self. So dealloc of sprite is called only when its retain count is 0. So removing it from array and self calls its dealloc. Check out above thread for more clear information.

« If just drawing with color rect then it may be box2d debug shape OR CC_SPRITE_DEBUG_DRAW is 1. If it is box2d shape then you need to destroy b2body before removing sprite.