I have two arrays: one for projectiles, and one for targets.
Every target and every projectile has tag, and a tag with color (source code I took from Ray Wanderlich).
In my small game, when a projectile collides with a target I compare their tags, if the tags are the same I delete the projectile and target, but if they are not I want to insert projectile at the position of the target (with which the projectile collided) and make the other targets shift right or left.
Some code:
NSMutableArray *projectilesToDelete = [[NSMutableArray alloc] init];
CCSprite * projectile;
for (projectile in _projectilesArray)
{
CGRect projectileRect = [projectile boundingBox];
NSMutableArray *targetsToDelete = [[NSMutableArray alloc] init];
CCSprite * target;
for (target in _targetsArray)
{
CGRect targetRect = [target boundingBox];
if (CGRectIntersectsRect(projectileRect, targetRect))
{
if([target tag]==[projectile tag])
{
[targetsToDelete addObject:target];
}
if([target tag]!=[projectile tag]) /* <-- _HERE IS MY PROBLEM_ */
{
NSLog(@"-");
[targetsToDelete addObject:projectile];
}
}
}
for (CCSprite *target in targetsToDelete)
{
[_targetsArray removeObject:target];
[self removeChild:target cleanup:YES];
}
if (targetsToDelete.count > 0)
{
[projectilesToDelete addObject:projectile];
}
}
for (CCSprite *projectile in projectilesToDelete)
{
[_projectilesArray removeObject:projectile];
[self removeChild:projectile cleanup:YES];
}