Cocos2d v3.0 - spritesheet的精灵动画

时间:2014-02-26 11:56:08

标签: cocos2d-iphone

如何在Cocos2d v3中使用SpriteAnimation?

下面的代码帮助了cocos2d 2.0,现在得到很多错误,比如CCAnimationCache,找不到CCAnimate。是否有任何cocos2d 3.0样本使用精灵帧进行动画?

    NSString *animName = @"FOG_ANIMATION";
    CCAnimation* animation = nil;

    animation = [[CCAnimationCache sharedAnimationCache]  animationByName:animName];

    if(!animation)
    {
        NSMutableArray *animFrames = [NSMutableArray array];
        CCSpriteFrameCache *cache = [CCSpriteFrameCache sharedSpriteFrameCache];

        for(int i=1;i<=18;i++)
        {
            NSString *file = [NSString stringWithFormat:@"Steam_%.2d.png",i];
            CCSpriteFrame *frame = [cache spriteFrameByName:file];

            if(frame)
            {
                [animFrames addObject:frame];
            }
        }

        animation = [CCAnimation animationWithSpriteFrames:animFrames];
        animation.delayPerUnit = DELAY_PER_FRAME_HERO_RUN;// 0.1f;
        animation.restoreOriginalFrame = NO;

        [[CCAnimationCache sharedAnimationCache] addAnimation:animation name:animName];
    }


    CCAnimate *AnimAction  = [CCAnimate actionWithAnimation:animation];
    CCRepeatForever *repAction = [CCRepeatForever actionWithAction:AnimAction];

    [steam runAction:repAction];

2 个答案:

答案 0 :(得分:0)

您也可以在Cocos2d v3中使用CCAnimationCache:

#import "cocos2d.h"
#import "cocos2d-ui.h"
#import "CCAnimation.h"
#import "CCAnimationCache.h"

-(void)animateBird
{
    NSString *animName = @"Bird_ANIM";

    CCAnimation* animation = nil;

    animation = [[CCAnimationCache sharedAnimationCache]  animationByName:animName];

    if(!animation)
    {
        NSMutableArray *animFrames = [NSMutableArray array];

        for(int i=1;i<=8;i++)
        {
            NSString *file = [NSString stringWithFormat:@"Hero_01_Flap_%.2d.png",i];
            CCSpriteFrame *frame = [cache spriteFrameByName:file];

            if(frame)
            {
                [animFrames addObject:frame];
            }
        }

        animation = [CCAnimation animationWithSpriteFrames:animFrames];

        animation.delayPerUnit = 0.13f;// 0.1f;
        animation.restoreOriginalFrame = NO;

        [[CCAnimationCache sharedAnimationCache] addAnimation:animation name:animName];
    }

    CCActionAnimate *AnimAction  = [CCActionAnimate actionWithAnimation:animation];
    CCActionRepeatForever *repAction = [CCActionRepeatForever actionWithAction:AnimAction];

    [bird runAction:repAction];
}

答案 1 :(得分:0)

转到此链接,它显示了如何制作精灵帧动画,希望这会有所帮助:D

Sprite Frame Animation Cocos2d 3.0