I Still getting erros, i try to explain better:
in my step of project:
-(void) doStep:(ccTime)dt
{
// Update the world of box2d
[self updateBox2DWorld:dt];
[self safelyRemoveBoxes];
}
And at my doStep of box2D World
-(void) updateBox2DWorld:(ccTime)dt
{
int32 velocityIterations = 5;
int32 positionIterations = 5;
_world->Step(dt, velocityIterations, positionIterations);
b2Body* node = _world->GetBodyList(); // Take advantage of the linked list
while(node)
{
b2Body* b = node; // Place 'node' into 'b' -
node = node->GetNext(); // Important, we grab the next 'node' now incase 'b' gets deleted
if(b->GetUserData() != NULL)
{
CGPoint cocosPosition = CGPointMake(b->GetPosition().x * PTM_RATIO,
b->GetPosition().y * PTM_RATIO);
float cocosRotation = -1 * CC_RADIANS_TO_DEGREES(b->GetAngle());
AtlasSprite* actor = (AtlasSprite *)b->GetUserData();
actor.position = cocosPosition;
actor.rotation = cocosRotation;
if ([[[(NSString *)b->GetUserData() class] description] isEqual:@"Box"])
{
Box *tempBox = (Box *)b->GetUserData();
if ((b->GetPosition().y * PTM_RATIO) < -10)
{
Box *tempBox = (Box *)b->GetUserData();
tempBox.bCountAsFall = YES;
[arrBoxToRemove addObject:tempBox];
}
}
}
}
}
my function to call box to remove:
-(void)safelyRemoveBoxes
{
for (Box *box in arrBoxToRemove)
{
[self removeBox:box];
}
[arrBoxToRemove removeAllObjects];
}
And at the removeBoxFunction:
-(void)removeBox:(Box *)pBox
{
b2Body *tempBody = [pBox getB2Body];
_world->DestroyBody(tempBody);
[asmBoxes removeChild:pBox cleanup:YES];
[arrAllBox removeObject:pBox];
}
But sometimes i get errors like this one:
But some times, i get "EXC_BAD_ACCESS" at the function:
b2Joint::Destroy(j, &m_blockAllocator);
So i try delete the joints before destroy the body:
-(void)removeBox:(Box *)pBox
{
b2Body *tempBody = [pBox getB2Body];
for (b2JointEdge* jointEdge = tempBody->GetJointList(); jointEdge != NULL; jointEdge = jointEdge->next)
{
b2Joint* targetJoint = jointEdge->joint;
_world->DestroyJoint(targetJoint);
}
_world->DestroyBody(tempBody);
[asmBoxes removeChild:pBox cleanup:YES];
[arrAllBox removeObject:pBox];
}
But i keep getting the error ramdomly.
Someone know what i doing wrong?