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);
}
首先分别定义三个动画传入动画缓存中,通过动画缓存获得这三个动画,然后定义动画动作,最后创建一个精灵并通过精灵运行动作。

你可能感兴趣的:(cocos2d-x类-动画)