I'm bouncing a dynamic body off of it so i know it's still there
Remove existing body (b2EdgeShape) when new body is created
General DiscussionOkay, then I guess you are not destroying the body you thought you were. How about some simple logging to check that you are actually using the right variables, eg:
Actually, I think I just noticed the problem... you are creating a new body every time the touch moves... so you probably have dozens of bodies, and you're only destroying the last one that was created. Shouldn't you only create a body in the touch down method?
Code:
// after creating the body
_brushBody = _world->CreateBody(&bd);
CCLOG(@"ccTouchesMoved: world %d created body %d", (int)_world, (int)_brushBody);
// before destroying body
CCLOG(@"ccTouchesBegan: world %d about to destroy body %d", (int)_world, (int)_brushBody);
_world -> DestroyBody(_brushBody);
_brushBody = _world->CreateBody(&bd);
CCLOG(@"ccTouchesMoved: world %d created body %d", (int)_world, (int)_brushBody);
// before destroying body
CCLOG(@"ccTouchesBegan: world %d about to destroy body %d", (int)_world, (int)_brushBody);
_world -> DestroyBody(_brushBody);
Actually, I think I just noticed the problem... you are creating a new body every time the touch moves... so you probably have dozens of bodies, and you're only destroying the last one that was created. Shouldn't you only create a body in the touch down method?
You're right!!!!! And now it works just as I want. Thank You! Thank You! I really appreciate your help
Wow I figured after finding the solution for destroying the body removing the sprites would be the easy part but they refuse to be removed. lol Placing the following code in ccTouchesBegan didn't work...
Code:
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if (_brushBody) {
for (int i = [lineSprites count]-1; i>=0; i--) {
CCSprite *oldBrush = [lineSprites objectAtIndex:0];
[self removeChild:oldBrush cleanup:YES];
}
[lineSprites removeAllObjects];
_world->DestroyBody(_brushBody);
_brushBody = NULL;
}
// create brush body
b2BodyDef bd;
bd.type = b2_staticBody;
bd.position.Set(0, 0);
_brushBody = _world->CreateBody(&bd);
}
if (_brushBody) {
for (int i = [lineSprites count]-1; i>=0; i--) {
CCSprite *oldBrush = [lineSprites objectAtIndex:0];
[self removeChild:oldBrush cleanup:YES];
}
[lineSprites removeAllObjects];
_world->DestroyBody(_brushBody);
_brushBody = NULL;
}
// create brush body
b2BodyDef bd;
bd.type = b2_staticBody;
bd.position.Set(0, 0);
_brushBody = _world->CreateBody(&bd);
}
That would be because you are only removing the first sprite in the 'lineSprites' array ( [lineSprites objectAtIndex:0] )
How about this:
How about this:
Code:
for (int i = 0; i < [lineSprites count]; i++) {
CCSprite *oldBrush = [lineSprites objectAtIndex:i];
[self removeChild:oldBrush cleanup:YES];
}
CCSprite *oldBrush = [lineSprites objectAtIndex:i];
[self removeChild:oldBrush cleanup:YES];
}
That didn't work either.
How about doing some basic debugging to see what's going on? Or at least some logging like I mentioned above, to track where the sprites are being added and removed? This is not a Box2D question any more, you might get more help at the cocos2d forums: cocos2d-iphone.org/forum
ok I'll do that. Thanks again for your help.
Original thread: https://box2d.org/forum/viewtopic.php?t=9281