Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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_ */
                [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];
share|improve this question
Do you mean something like Zuma works? – Kromster Jul 20 '11 at 11:39
yes something like zuma – Darth Vader Jul 20 '11 at 11:45
up vote 1 down vote accepted

Isn't this a simple programming problem?

Change your for in loop to a standard for loop, then in your problem area use [targetsToDelete addObject:*something* atIndex:*i*]

share|improve this answer
I would guess the question involves animation, etc. otherwise yes, totally agree. – Jonathan Connell Jul 20 '11 at 17:01
Good call. I almost never use for in loops. They are less flexible, and run slower. – AttackingHobo Jul 20 '11 at 19:39
Oh, thanks. I tried get index of object with [targetsToDelete indexOfObject:*something*]; and after that insert new object at this index. – Darth Vader Jul 22 '11 at 6:46

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.