Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to animate CCSprite in Cocos2D 3.x?

do you know how to animate a CCSprite in the new Cocos2D v3.x ?

A lot of classes are changed, and old method seems not work.

NSMutableArray *animFrames = [NSMutableArray array];
    for(int i = 1; i <= 3; i++) {
        CCSpriteFrame *frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"Sprite-%d.png",i]];
        [animFrames addObject:frame];
    }
    CCAnimation *animation = [CCAnimation animationWithName:@"run" delay:0.1f frames:animFrames];
    [mySprite runAction:[CCRepeatForever actionWithAction: [CCAnimate actionWithAnimation:animation restoreOriginalFrame:NO]]];

any idea?

thanks.


extra info

enter image description here

like image 836
elp Avatar asked Feb 08 '14 12:02

elp


People also ask

How do you add animations to Sprite Godot?

Select the AnimationPlayer and click the “Animation” button followed by “New”. Name the new animation “walk”. Set the animation length to 0.6 and click the “Loop” button so that our animation will repeat. Now select the Sprite node and click the key icon to add a new track.


2 Answers

This is how it works:

    NSMutableArray *animationFrames = [NSMutableArray array];

    for(int i = 1; i <= FRAMES; ++i)
    {
        CCSpriteFrame *spriteFrame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: [NSString stringWithFormat:@"animationFrame%d.png", i]]; //
    }

    //Create an animation from the set of frames you created earlier
    CCAnimation *animation = [CCAnimation animationWithSpriteFrames: animationFrames delay:delay];

    //Create an action with the animation that can then be assigned to a sprite
    CCActionAnimate *animationAction = [CCActionAnimate actionWithAnimation:animation];

    CCActionRepeatForever *repeatingAnimation = [CCActionRepeatForever actionWithAction:animationAction];
    [self runAction:repeatingAnimation];
like image 140
Ben-G Avatar answered Oct 13 '22 05:10

Ben-G


You could change CCRepeatForever to CCActionRepeatForever.

like image 36
user3276504 Avatar answered Oct 13 '22 06:10

user3276504