Search code examples
iosuiimageviewcocos2d-iphoneccsprite

.animationImages API Cocos 2D?


I am looking to do something similar to the API in UIImageView where you supply say 5 images and through a certain duration the UIImageView will switch through those images. I am looking to do this in a CCSprite but the only ways I hear of doing this is customizing frames of the Node or something like that.

Does someone know an easy way to achieve something like this where I provide say 5 images and I want it to cycle though them in 4 seconds?

Thanks!


Solution

  •     //initialize anitmation
        CCAnimation *anime= [[CCAnimation alloc] initWithName:@"anime" delay:4.0];
    
        for(int i = 1; i <= 5; i++){
                [anime addFrameWithFilename:[NSString  stringWithFormat:@"frame%d.png", i]];
        }
    
        id animeAction = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:anime]];
    
       [self runAction:animeAction];
    

    It takes 5 images and animate them in 4.0 periods.

    ----- Edit ----- Here is how can you do it with new Cocos2d Api 1.0.1 :

        //initialize anitmation
        CCAnimation *anime= [CCAnimation animation];
        anime.delay = 4.0;
    
        for(int i = 1; i <= 5; i++){
                [anime addFrameWithFilename:[NSString  stringWithFormat:@"frame%d.png", i]];
        }
    
        id animeAction = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:anime]];
    
       [self runAction:animeAction];