cocos2d-x学习之通过plist实现动画

通过贴图集plist文件定义CCSpriteFrameCache或通过动画plist文件定义CCAnimationCache
AnimationCache::AnimationCache()
{
    //创建精灵帧缓存指针
    CCSpriteFrameCache *frameCache = CCSpriteFrameCache::sharedSpriteFrameCache();
    //向缓存中存入精灵帧
    frameCache->addSpriteFramesWithFile("animations/grossini.plist");
    frameCache->addSpriteFramesWithFile("animations/grossini_gray.plist");
    frameCache->addSpriteFramesWithFile("animations/grossini_blue.plist");

    //
    // create animation "dance"
    //
    CCArray* animFrames = CCArray::createWithCapacity(15);//帧数组
    char str[100] = {0};
    for(int i = 1; i < 15; i++)
    {
        sprintf(str, "grossini_dance_%02d.png",i);
        CCSpriteFrame *frame = frameCache->spriteFrameByName(str);//提取plist文件中单个帧
        animFrames->addObject(frame);//向帧数组中添加帧
    }

    //通过帧数组创建动画
    CCAnimation *animation = CCAnimation::createWithSpriteFrames(animFrames, 0.2f);

    // Add an animation to the Cache,named as dance
    CCAnimationCache::sharedAnimationCache()->addAnimation(animation, "dance");

    //
    // create animation "dance gray"
    //
    animFrames->removeAllObjects(); //清空数组

    for(int i = 1; i < 15; i++)
    {
        sprintf(str, "grossini_dance_gray_%02d.png",i);
        CCSpriteFrame *frame = frameCache->spriteFrameByName(str);
        animFrames->addObject(frame);
    }

    animation = CCAnimation::createWithSpriteFrames(animFrames, 0.2f);

    // Add an animation to the Cache
    CCAnimationCache::sharedAnimationCache()->addAnimation(animation, "dance_gray");

    //
    // create animation "dance blue"
    //
    animFrames->removeAllObjects();

    for(int i = 1; i < 4; i++)
    {
        sprintf(str, "grossini_blue_%02d.png",i);
        CCSpriteFrame *frame = frameCache->spriteFrameByName(str);
        animFrames->addObject(frame);
    }
    animation = CCAnimation::createWithSpriteFrames(animFrames, 0.2f);

    // Add an animation to the Cache
    CCAnimationCache::sharedAnimationCache()->addAnimation(animation, "dance_blue");


    CCAnimationCache *animCache = CCAnimationCache::sharedAnimationCache();

    //动画缓存指针通过动画名字确定动画
    CCAnimation *normal = animCache->animationByName("dance");
    normal->setRestoreOriginalFrame(true);//是否回到第一帧

    CCAnimation *dance_grey = animCache->animationByName("dance_gray");
    dance_grey->setRestoreOriginalFrame(true);

    CCAnimation *dance_blue = animCache->animationByName("dance_blue");
    dance_blue->setRestoreOriginalFrame(true);

    //通过动画创建动画动作
    CCAnimate *animN = CCAnimate::create(normal);
    CCAnimate *animG = CCAnimate::create(dance_grey);
    CCAnimate *animB = CCAnimate::create(dance_blue);
    //组合动作
    CCSequence *seq = CCSequence::create(animN, animG, animB, NULL);

    // create an sprite without texture
    CCSprite *grossini = CCSprite::create();
    CCSpriteFrame *frame = frameCache->spriteFrameByName("grossini_dance_01.png");
    grossini->setDisplayFrame(frame);	//设置显示帧

    CCSize winSize = CCDirector::sharedDirector()->getWinSize();
    grossini->setPosition(ccp(winSize.width/2, winSize.height/2));
    addChild(grossini);

    // run the animation
    grossini->runAction(seq);
}
首先分别定义三个动画传入动画缓存中,通过动画缓存获得这三个动画,然后定义动画动作,最后创建一个精灵并通过精灵运行动作。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值