1
votes

I am creating animation in sprite.I am changing sprite after perticular period and repeating it forever.I have referred many site and the format is same as below:

-(id) init  
{  
    if( (self=[super init] )) 
    {   

        CCSpriteSheet *spriteSheet = [CCSpriteSheet   spriteSheetWithFile:@"apls.png" capacity:3]; 

        [[CCSpriteFrameCache sharedSpriteFrameCache]   addSpriteFramesWithFile:@"apls.plist"]; 

        CCSprite *sprite = [CCSprite spriteWithSpriteFrameName:@"apple2.png"]; 
        sprite.position = ccp(230,230);                     
        [spriteSheet addChild:sprite];  
        [self addChild:spriteSheet];  


        NSMutableArray *animFrames = [NSMutableArray array];  
        for(int i = 2; i < 5; i++)  
        {  

            CCSpriteFrame *frame = [[CCSpriteFrameCache sharedSpriteFrameCache] 
                        spriteFrameByName: [NSString stringWithFormat:@"apple%d.png",i]];  
            [animFrames addObject:frame];  

        }  

        CCAnimation *animation = [CCAnimation animationWithName:@"dance"      
                                delay:0.2f  frames:animFrames];  

        [sprite runAction:[CCRepeatForever actionWithAction: 
        [CCAnimate  actionWithAnimation:animation restoreOriginalFrame:NO] ]];  

    }  
    return self;  
}

this is the code for the same. But on simulator it is not showing anything. just frame rate keeps on changing.
Is there something which I am missing?

thanks in advance.

1
is your sprite displayed if you'll comment all the animation code ? - Andrew
yes, without animation it is showing other sprites CCSprite *p=[CCSprite spriteWithFile:@"apple1.png"]; p.position=ccp(230,230); [self addChild:p z:0 tag:1]; this code is displaying apple1 image but animation code is not working. - user392406
There is frame animation example coming with cocos2d (in spriteTest example). Take a look there - Andrew
If possible can you plzz send me the link I am not finding it.. - user392406
I have tried that example but the result is same. It is not showing anything except the frame rate..:( - user392406

1 Answers

1
votes

Try this.

NSMutableArray *animFrames = [NSMutableArray array];  
    for(int i = 2; i < 5; i++)  
    {  

        CCSpriteFrame *frame = [[CCSpriteFrameCache sharedSpriteFrameCache] 
                    spriteFrameByName: [NSString stringWithFormat:@"apple%d.png",i]];  
        [animFrames addObject:frame];  
    }

CCAnimation *animation = [CCAnimation frames:animFrames delay:0.2f]; 

[sprite runAction:[CCRepeatForever actionWithAction: [CCAnimate actionWithAnimation:animation restoreOriginalFrame:NO] ]];