【问题标题】:Cocos2d v3.0 - Sprite animation from spritesheetCocos2d v3.0 - 来自 spritesheet 的 Sprite 动画
【发布时间】:2014-03-29 05:40:26
【问题描述】:

如何在Cocos2d v3中使用SpriteAnimation?

以下代码在 cocos2d 2.0 中有所帮助,现在出现许多错误,例如 CCAnimationCache、CCAnimate not found。有没有使用精灵帧动画的 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];

【问题讨论】:

    标签: cocos2d-iphone


    【解决方案1】:

    你也可以在 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];
    }
    

    【讨论】:

      【解决方案2】:

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

      Sprite Frame Animation Cocos2d 3.0

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-05-16
        • 2014-03-17
        • 2014-05-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-11-18
        相关资源
        最近更新 更多