Please help understand error: body->space != space

Official forum for the Chipmunk2D Physics Library.
Post Reply
mgs
Posts: 1
Joined: Sun Mar 30, 2014 5:14 pm
Contact:

Please help understand error: body->space != space

Post by mgs »

Hi all,

I'm very new to cocos2d and chipmunk, but hoping someone would be able to help me understand an error I'm getting.

Some background:

In the game I'm making I have 1 "Play Level", which I restart for each new "round" of my game, incrementing score and storing global variables in App Delegate (as central storage). So play level resets everything for the next round, but still remembers that player had progressed in score, etc.

I use SpriteBuilder (with the latest Cocos/Chipmunk engine that it comes with).

The game has a road and cars in it. I create the road and CCPhysicsNode in SpriteBuilder, but then randomly choose car sprites (defined in SpriteBuilder too) by name (e.g. "car4") and attach them to the CCPhysicsNode when the new round starts.

So,

1. I populate array of (available) car sprites:

carSprites addObject:(CCSprite *)[CCBReader load:[NSString stringWithFormat:@"%@%i", carUnlock.spriteName, i]]];

2. Then I randomly pick one, and create my custom class (as somehow it didn't work creating Car object directly with 'CCBReader load' - so I had to do a bit of a dance around here:

// get the sprite from Sprite Builder, from the total number of available ones
CCSprite *carSprite = (CCSprite *)[carSprites objectAtIndex:(int)arc4random_uniform(maxNumberOfCarSprites)];

// now create my custom Car object from retrieved CCSprite
Car *car = [[Car alloc] initWithAnotherSprite:carSprite impulseStrength:(CGFloat)arc4random_uniform(MAX_RANDOM_IMPULSE)];

3. and then I add it to the physics body after setting it's position and all...

[_physicsNode addChild:car];


Now this seems to be working fine, i.e. there is NO ERROR or crash doing any of the above.

But then I almost immediately get this error:

Code: Select all

Aborting due to Chipmunk error: You have already added this body to this space. You must not add it a second time.
    Failed condition: body->space != space
    Source:/Users/mgs/MyCarsGame.spritebuilder/Source/libs/cocos2d-iphone/external/Chipmunk/src/cpSpace.c:443
I do not do any more addChild to physicsNode anywhere! It drives me nuts as I've no idea what's causing this?

If this is still unclear (which I hope it won't be), maybe at least could you explain what's that failing assertion trying to do?

Any ideas would be most appreciated at this stage...

Thanks.
Post Reply

Who is online

Users browsing this forum: No registered users and 30 guests