program crashes when removing shapes in spacemanager - cocos2d
i am using spacemanager with chipmunk physics for my game. What i am trying to do is hit a ball to a pillar and make the ball disappear. i am recreating the ball in different location so the user can hit another pillar. i have the ball shape and ball sprite as member variable.
Here is the code for creating ball, pillar shapes etc.
ball = [smgr addCircleAt:cpv(1000,10) mass:0.5 radius:35];
ball->collision_type = kBallCollisionType;
ballSprite = [cpCCSprite spriteWithShape:ball file:@"head.png"];
//[ballSprite autoFreeShape];
[self addChild:ballSprite];
ballSprite.spaceManager = smgr;
//ballSprite.ignoreRotation = NO;
cpShape *dome = [smgr addRectAt:cpv(400,500) mass:1 width:400 height:100 rotation:0];
dome->collision_type = kRectCollisionType;
cpCCSprite *dome1 = [cpCCSprite spriteWithShape:dome file:@"001.png"];
[self addChild:dome1];
dome1.spaceManager = smgr;
cpShape *pillarone = [smgr addRectAt:cpv(300,300) mass:1 width:45 height:194 rotation:0开发者_JS百科];
pillarone->collision_type = kRectCollisionType;
cpCCSprite *pillar1 = [cpCCSprite spriteWithShape:pillarone file:@"004.png"];
[self addChild:pillar1];
pillar1.spaceManager = smgr;
cpShape *pillartwo = [smgr addRectAt:cpv(500,300) mass:1 width:45 height:194 rotation:0];
pillartwo->collision_type = kRectCollisionType;
cpCCSprite *pillar2 = [cpCCSprite spriteWithShape:pillartwo file:@"004.png"];
[self addChild:pillar2];
pillar2.spaceManager = smgr;
cpShape *staticground = [smgr addRectAt:cpv(510,25) mass:1 width:0 height:0 rotation:0];
cpCCSprite *staticground1 = [cpCCSprite spriteWithShape:staticground file:@"grass1-1024.png"];
[self addChild:staticground1 z:1 tag:0];
[smgr addCollisionCallbackBetweenType:kRectCollisionType
otherType:kBallCollisionType
target:self
selector:@selector(handleCollisionWithFragmentingRect:arbiter:space:)];
AND HERE IS THE CODE FOR COLLISION HANDLING.
- (void) handleCollisionWithFragmentingRect:(CollisionMoment)moment arbiter:(cpArbiter*)arb space:(cpSpace*)space{
if (moment == COLLISION_POSTSOLVE)
{
[self removeChild:ball->data cleanup:YES];
[smgr removeAndFreeShape:ball];
ball = [smgr addCircleAt:cpv(1000,10) mass:0.5 radius:35];
ball->collision_type = kBallCollisionType;
ballSprite = [cpCCSprite spriteWithShape:ball file:@"head.png"];
[self addChild:ballSprite];
ballSprite.spaceManager = smgr;
}
}
When the first ball hits the pillar it disappears fine. but for the second and some times third ball i pick and hit the pillar it crashes with error as follows.
Chipmunk warning: Cannot remove a constraint that was not added to the space. (Removed twice maybe?)
Failed condition: cpArrayContains(space->constraints, constraint)
i am not sure where i went wrong, can anyone help please.
Thanks.
I think you need to add teh ball to be removed to a "garbage" array, and not remove it on the handleCollisionWithFragmentingRect function. For all safety measures, include the
- (void) update : (ccTime) dt
{
for (cpShape *shape in _garbage)
{
// Remove shape
[self removeChild:shape->data cleanup:YES];
[smgr removeAndFreeShape:shape];
// Add new ball
ball = [smgr addCircleAt:cpv(1000,10) mass:0.5 radius:35];
ball->collision_type = kBallCollisionType;
ballSprite = [cpCCSprite spriteWithShape:ball file:@"head.png"];
[self addChild:ballSprite];
ballSprite.spaceManager = smgr;
}
...
}
And then you would change the handle function to this:
- (void) handleCollisionWithFragmentingRect:(CollisionMoment)moment arbiter:(cpArbiter*)arb space:(cpSpace*)space
{
if (moment == COLLISION_POSTSOLVE)
{
// SHOULDNT BE A DIRECT REFERENCE TO MEMBER VARIABLE BALL.
// SHOULD BE ADDED FROM INFORMATION WITHIN THE PARAMETERS OF THIS FUNCTION.
_garbage.addObject(ball);
}
}
精彩评论