I am doing some thing like this.
// these are my collision functions.
static void CollisionPowerUpRemove(cpSpace *space, cpShape *shape, void *MyBGPowerup)
{
CCSprite *MyBG = (CCSprite *)MyBGPowerup;
CCSprite *tempSprite = (CCSprite *) shape->data;
[MyBG removeChild:tempSprite cleanup:YES];
cpSpaceRemoveBody(space, shape->body);
cpBodyFree(shape->body);
cpSpaceRemoveShape(space, shape);
cpShapeFree(shape);
// here I remove a power from NSMutableArray
for(int u=0; u<[PowerUPs count]; u++)
{
PowerUp *tempPU = (PowerUp *)[PowerUPs objectAtIndex:u];
if(shape->body == tempPU.PowerUpBody)
{
[tempPU RemoveMe];
[PowerUPs removeObject:tempPU];
u = [PowerUPs count];
}
}
}
static int EnemyCollision(cpArbiter *arb, cpSpace *space, void *GamePlayClass)
{
gamePlay *ClassObj = (gamePlay *) GamePlayClass; //this is my game play class
cpShape *a, *b;
cpArbiterGetShapes(arb, &a, &b);
cpSpaceAddPostStepCallback(space, (cpPostStepFunc)CollisionPowerUpRemove, b, ClassObj.bg );
a->body->v = cpvzero; // it stops my player
[ClassObj DeleteARandomEnemy];
return 1;
}
when I call this [ClassObj DeleteARandomEnemy]; using this i have to delete one of the enemy in it I do like this. Here the function "DeleteARandomEnemy" is in my game play class. Here I select a random enemy and call the delete function of enemy class.
-(void) DeleteARandomEnemy
{
int EnemyCount = [Enemies count];
EnemyCount -= 1;
int IndexToRemove = (arc4random() % EnemyCount)+0;
Enemy *ObjEnemyRemove = (Enemy *)[Enemies objectAtIndex:IndexToRemove];
[ObjEnemyRemove DeleteMe:bg MySpace:space]; // here bg is background of game play
[Enemies removeObject:ObjEnemyRemove];
}
the Delete Me function in Enemy class is
-(void) DeleteMe:(CCSprite *)bg MySpace:(cpSpace *)MySpace
{
CCSprite *tempSprite = (CCSprite *) self.EnemyShape->data;
[bg removeChild:tempSprite cleanup:YES];
cpSpaceRemoveBody(MySpace, self.EnemyBody);
cpBodyFree(self.EnemyBody);
cpSpaceRemoveShape(MySpace, self.EnemyShape);
cpShapeFree(self.EnemyShape);
}
@mobilebros I have tried to use
cpSpacePostStepRemoveAndFreeShapeAndBody(space, shape);
but it gives error.
@Mark I am already using cpSpaceAddPostStepCallback. it removes the body of collision fine. the problem is only when I delete a random displayed Enemy from the game play.
if some is unable to understand yet. i can explain more.
thanks for reply guys