Search code examples
cocos2d-iphonebox2d-iphone

How to run two animation at same time at same sprite?


In my game, I am running two animations using CCSpawn but it shows only one animation at a time. What is wrong here? Here is my code.

[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"walkcycle.plist"] ;
CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"walkcycle.png"];
[heroWorldLayer addChild:spriteSheet];

NSMutableArray *runFrames = [NSMutableArray array];
for(int i = 1; i <= 11; ++i) {
    CCSpriteFrame *frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"Run_Anim00%02d.png", i]];
    [runFrames addObject:frame];
}
id runAnim = [CCAnimation animationWithFrames:runFrames delay:1.0/22.0];
id runAnimate = [CCAnimate actionWithAnimation:runAnim restoreOriginalFrame:NO];
//    _runAction = [CCRepeatForever actionWithAction:runAnimate];
//    [heroBodySprite runAction:_runAction];

NSMutableArray *poofFrames = [NSMutableArray array];
for(int i = 1; i <= 10; ++i) {
    CCSpriteFrame *frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"Poof00%02d.png", i]];
    [poofFrames addObject:frame];
}
id poofAnim = [CCAnimation animationWithFrames:poofFrames delay:1.0/20.0];
id poofAnimate = [CCAnimate actionWithAnimation:poofAnim restoreOriginalFrame:NO];
//    id poofAction = [CCRepeatForever actionWithAction:poofAnimate];
//    [heroBodySprite runAction:poofAction];

[heroBodySprite runAction:[CCRepeatForever actionWithAction:[CCSpawn actions:runAnimate, poofAnimate, nil]]];

Solution

  • You basically want to display two different frames at the same time on the same sprite. That is just not possible, think about it. What kind of magic do you expect Cocos2d to do for you ? Some animations are just not made to be compatible with each other. It's like if you tried to move a sprite to the left and to the right at the same time, and were surprised it didn't work...