Cocos2Dx(4)——动画

1. Cocos2d-x中动画相关的类。

1.1 精灵帧类CCSpriteFrame,表示精灵动画中的一帧,通常由贴图定义。

1.2 精灵缓存类CCSpriteFrameCache,用于缓存精灵帧以提高效率,是单实例模式,所有精灵共享同一个缓存类实例。通过CCSpriteFrameCache::sharedSpriteFrameCache()获得。

1.3 动画帧类CCAnimationFrame,表示动画中的一帧,通过精灵帧定义

1.4 动画类CCAnimation,储存一个动画的所有帧,可以通过帧数组定义。并且定义了帧间隔时间。

1.5 动画缓存类CCAnimationCache,缓存动画和动画帧,是单实例模式,所有动画共享一个i额缓存类实例,通过CCAnimationCache::sharedAnimationCache()获得。

1.6 动画动作类CCAnimate,继承自CCActionInterval,通过CCAnimation类定义。

2. 不经过缓存时,创建一个动画的过程。

(1) 定义精灵帧。

(2) 使用精灵帧定义动画帧。

(3) 通过动画帧数组定义动画(也可以直接通过精灵帧数组定义动画)。

(4) 通过动画定义动画动作,通过精灵执行动画动作播放相应动画。

示例代码如下:

    //创建精灵帧
    CCSpriteFrame* spriteFrame1 = CCSpriteFrame::create("card1037.png", CCRectMake(0,0, 200,200));
    CCSpriteFrame* spriteFrame2 = CCSpriteFrame::create("card1040.png", CCRectMake(0,0, 200,200));

    //通过精灵帧定义动画帧
    CCAnimationFrame* animFrame1 = new CCAnimationFrame();
    CCAnimationFrame* animFrame2 = new CCAnimationFrame();
    animFrame1->initWithSpriteFrame(spriteFrame1, 1, NULL);
    animFrame2->initWithSpriteFrame(spriteFrame2, 1, NULL);

    //构建动画帧数组
    CCArray* array = CCArray::createWithCapacity(15); 
    array->addObject(animFrame2);
    array->addObject(animFrame1);

    //通过动画帧数组定义动画
    CCAnimation *animation = CCAnimation::create(array, 0.2f);
  //也通过精灵帧数组直接定义动画
  //CCAnimation::createWithSpriteFrame(array, 0.2f);
//通过动画定义动画动作 CCAnimate *animate = CCAnimate::create(animation);//精灵执行动画动作 CCSprite *ani_sprite = CCSprite::create(); ani_sprite->setPosition(ccp(size.width/2, size.height/2)); ani_sprite->runAction(CCRepeatForever::create(animate));

3. 通常将精灵帧和动画提前缓存起来可以提高程序效率,相关操作主要是将精灵帧加入缓存和从缓存中取精灵帧。

在定义精灵帧的时候将精灵帧加入缓存,addSpriteFrame():

CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFrame(spriteFrame1, "frame_1");

需要时从缓存中获取精灵帧,spriteFrameByName():

array->addObject(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("frame_1"));

定义动画时将动画加入动画缓存,addAnimation():

CCAnimationCache::sharedAnimationCache()->addAnimation(animation, "duke");

需要时从缓存读取动画,animationByName():

CCAnimate *animate = CCAnimate::create(CCAnimationCache::sharedAnimationCache()->animationByName("duke"));

3. 通常可以将多个帧的图片使用工具压缩到一个图片中,使得代码只需要加载一次,通常使用Texture Packer工具(http://www.codeandweb.com/texturepacker);

使用CCSpriteFrameCache从文件中读取动画相关图片信息:

framCache->addSpriteFramesWithFile("beauty/beauty.plist");

其中.plist是由Texture Packer工具处理图片产生的,存储了动画图片的自定义信息。

//从plist文件中读取帧信息并放到精灵帧缓存
    framCache->addSpriteFramesWithFile("beauty/beauty.plist");

    //精灵帧数组
    CCArray* array = CCArray::createWithCapacity(15); 
    for (int i = 1;i<=10;i++)
    {
        sprintf(frameName, "beauty%02d.png", i);
        CCSpriteFrame* frame = framCache->spriteFrameByName(frameName);
        CCAnimationFrame *aniFrame = new CCAnimationFrame();
        aniFrame->initWithSpriteFrame(frame, 1, NULL);
        array->addObject(frame);
    }

    //精灵帧数组直接定义动画
    CCAnimation *animation = CCAnimation::createWithSpriteFrames(array, 0.1f);

    //定义动画动作
    CCAnimate *animate = CCAnimate::create(animation);

    //精灵执行动作
    CCSprite *ani_sprite = CCSprite::create();
    ani_sprite->setPosition(ccp(size.width/2, size.height/2));
    ani_sprite->runAction(CCRepeatForever::create(animate));
原文地址:https://www.cnblogs.com/litterrondo/p/3098839.html