智慧 + 毅力 = 无所不能

正确性、健壮性、可靠性、效率、易用性、可读性、可复用性、兼容性、可移植性...
随笔 - 991, 文章 - 0, 评论 - 27, 阅读 - 341万

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

cocos2d-x Animation

Posted on   Bill Yuan  阅读(3551)  评论(0编辑  收藏  举报

转自:http://codingnow.cn/cocos2d-x/810.html

这一篇来学习怎么使用cocos2d-x引擎播放帧动画,就是把一帧一帧的图片像电影那样显示出来。
1. 首先来了解一下相关的类
CCAnimation:是精灵用来播放动画的参数,内部封装了一个帧序列(CCMutableArray<CCSpriteFrame*>)和每帧播放间隔时间(float m_fDelay),初始化该对象时记得指定delay时间,否则默认是0。
CCAnimationCache:从名字很容易看出,它是用来缓存CCAnimation的,内部封装了一个字典(CCMutableDictionary<std::string, CCAnimation*>),是一个单例。使用实例:

CCMutableArray<CCSpriteFrame *>* pAnimFrames = new CCMutableArray<CCSpriteFrame *>(10);
/** .... */
CCAnimation* pAnimation = CCAnimation::animationWithFrames( pAnimFrames, 0.12f);
//添加到缓存
CCAnimationCache::sharedAnimationCache()->addAnimation(pAnimation,"conch_animation");
//从缓存取出
CCAnimation* pAnimation2 =CCAnimationCache::sharedAnimationCache()->animationByName("conch_animation")

CCAnimate:它的父类是CCActionInterval,作用是根据CCAnimation封装的帧序列和间隔时间,使精灵产生动画效果。

cocos2d-x播放帧动画的主要的流程是:
(1)创建CCSpriteFrame数组,可以使用CCSpriteFrameCache或者CCTextureCache。
(2)通过帧序列创建CCAnimation对象
(3)通过CCAnimation对象和间隔时间创建CCAnimate,生成一个持续性动作。
(4)使用精灵执行动作

2.下面分别使用CCSpriteFrameCache和CCTextureCache来实现一个播放动画的demo
(1)使用CCSpriteFrameCache获得动画帧,可以使用TexturePacker工具把多个分散的小图集中到一张大图上,然后生成plist文件。程序中使用的图片:

下面是播放动画的核心代码:

复制代码
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
CCSpriteFrameCache* pFrameCache = CCSpriteFrameCache::sharedSpriteFrameCache();
pFrameCache->addSpriteFramesWithFile(s_plistPathPropConch, s_imgPathPropConch);
 
m_pHero = CCSprite::spriteWithSpriteFrameName("ani_conch_1.png");
m_pHero->retain();
this->addChild(m_pHero);
m_pHero->setPosition( ccp(winSize.width/2,winSize.height/2) );
 
const int frameCount = 4;
CCMutableArray<CCSpriteFrame *>* pAnimFrames = new CCMutableArray<CCSpriteFrame *>(frameCount);
char str[50] = {0};
for(int i = 0; i != frameCount; ++i)
{
    sprintf(str, "ani_conch_%d.png", i+1);  
    CCLog("str=%s",str);
    CCSpriteFrame* pFrame = pFrameCache->spriteFrameByName( str ); 
    pAnimFrames->addObject( pFrame );
}
 
//delay默认是0
CCAnimation* pAnimation = CCAnimation::animationWithFrames( pAnimFrames, 0.12f);
pAnimFrames->release();
m_pHero->runAction( CCRepeatForever::actionWithAction( CCAnimate::actionWithAnimation(pAnimation,false) ) );
复制代码

sprintf的作用是字符串格式化,主要功能是把格式化的数据写入某个字符串中。sprintf(str, “ani_conch_%d.png”, 1)后str的值就变成了:ani_conch_1.png,其他使用方法可以去google或者百度查。

(2)使用CCTextureCache获得动画帧,程序中用到的图片:

 

核心代码如下:

复制代码
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
 
CCTexture2D* pTexture = CCTextureCache::sharedTextureCache()->addImage( s_imgPathKnight );
int textureWidth = pTexture->getContentSize().width;
int textureHeight = pTexture->getContentSize().height;
CCLog("width=%d,height=%d",textureWidth, textureHeight);
const int row = 4,col = 4;
int spriteWidth = textureWidth / row, spriteHeight = textureHeight / 4;
 
CCSpriteFrame* pHeroFrame = CCSpriteFrame::frameWithTexture(pTexture,CCRectMake(0,0,spriteWidth,spriteHeight));
m_pHero = CCSprite::spriteWithSpriteFrame(pHeroFrame);
m_pHero->retain();
this->addChild(m_pHero);
m_pHero->setPosition( ccp(winSize.width/2,winSize.height/2) );
 
CCMutableArray<CCSpriteFrame *>* pAnimFrames = new CCMutableArray<CCSpriteFrame *>(16);
for(int i = 0; i != col; ++i)
{
    CCSpriteFrame* pSpriteFrame = CCSpriteFrame::frameWithTexture(pTexture,CCRectMake(spriteWidth * i, 0, spriteWidth, spriteHeight));
    pAnimFrames->addObject(pSpriteFrame);
}
 
//delay默认是0
CCAnimation* pAnimation = CCAnimation::animationWithFrames( pAnimFrames, 0.2f);
pAnimFrames->release();
m_pHero->runAction( CCRepeatForever::actionWithAction( CCAnimate::actionWithAnimation(pAnimation,false) ) );
复制代码

 

(评论功能已被禁用)
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· [AI/GPT/综述] AI Agent的设计模式综述
点击右上角即可分享
微信分享提示