0
votes

I have created two bounding boxes in cocos2d for iPhone and when they intersect the image from selSprite replaces the image in targetSprite if their tags match. I want to give a nice animation when the bounding boxes collide. The following code replaces the sprites successfully but the FadeOut animation runs AFTER the images have been replaced. The animation should run when the images are being replaced. What am I doing wrong? Any help would be appreciated. Thanks!

if (CGRectIntersectsRect(getSpriteRect, selSpriteRect)) {

        if ([selSprite tag] == ([targetSprite tag]-7)) {
            //NSLog(@"%d", [targetSprite tag]);
            //NSLog(@"%@", [selectedSpritesToBeUsedLater objectAtIndex:[selSprite tag]]);

            CCSprite *replacedImage = (CCSprite *)[self getChildByTag:[targetSprite tag]];

            CCSprite *spriteToBeUsedInTex = [selectedSpritesToBeUsedLater objectAtIndex:[selSprite tag]];

            id removeSpriteAction = [CCFadeOut actionWithDuration:2.0f];

            id removeSelSprite = [CCCallFunc actionWithTarget:self selector:@selector(removeSelSpriteByTag)];

            CCSequence *sequence = [CCSequence actions:removeSpriteAction, removeSelSprite, nil];

            [selSprite runAction:sequence]; 

            CCTexture2D *tex = [[CCTextureCache sharedTextureCache] addImage:[NSString stringWithFormat:@"color-0%d.png", [spriteToBeUsedInTex tag]+1]];

            [replacedImage setTexture:tex];

        }
    }
} }

-(void)removeSelSpriteByTag {
[self removeChildByTag:[selSprite tag] cleanup:YES]; }
1

1 Answers

1
votes

You should use CCSpawn instead of CCSequence then.