一个精灵的动画该怎么理解?
我的理解就是场景中原本死气沉沉的精灵在原地动起来了。
CCAnimation和CCAnimate的官方源代码解释是下面这段话【版本cocos2dx-2.2.2】

/** A CCAnimation object is used to perform animations on the CCSprite objects.
The CCAnimation object contains CCAnimationFrame objects,and a possible delay between the frames.
You can animate a CCAnimation object by using the CCAnimate action. Example:
[sprite runAction:[CCAnimate actionWithAnimation:animation]];
*/
class CC_DLL CCAnimation : public CCObject
{
public:
/**
* @js ctor
*/
CCAnimation();
/**
* @js NA
* @lua NA
*/
~CCAnimation(void);
public:
/** Creates an animation
@since v0.99.5
*/
static CCAnimation* create(void);
/* Creates an animation with an array of CCSpriteFrame and a delay between frames in seconds.
The frames will be added with one "delay unit".
@since v0.99.5
@js create
*/
static CCAnimation* createWithSpriteFrames(CCArray* arrayOfSpriteFrameNames,float delay = 0.0f);
/* Creates an animation with an array of CCAnimationFrame,the delay per units in seconds and and how many times it should be executed.
@since v2.0
*/
static CCAnimation* create(CCArray *arrayOfAnimationFrameNames,float delayPerUnit,unsigned int loops);
static CCAnimation* create(CCArray *arrayOfAnimationFrameNames,float delayPerUnit) {
return CCAnimation::create(arrayOfAnimationFrameNames,delayPerUnit,1);
}
/** Adds a CCSpriteFrame to a CCAnimation.
The frame will be added with one "delay unit".
*/
void addSpriteFrame(CCSpriteFrame *pFrame);
/** Adds a frame with an image filename. Internally it will create a CCSpriteFrame and it will add it.
The frame will be added with one "delay unit".
Added to facilitate the migration from v0.8 to v0.9.
* @js addSpriteFrameWithFile
*/
void addSpriteFrameWithFileName(const char *pszFileName);
/** Adds a frame with a texture and a rect. Internally it will create a CCSpriteFrame and it will add it.
The frame will be added with one "delay unit".
Added to facilitate the migration from v0.8 to v0.9.
*/
void addSpriteFrameWithTexture(CCTexture2D* pobTexture,const CCRect& rect);
/**
* @lua NA
*/
bool init();
/** Initializes a CCAnimation with frames and a delay between frames
@since v0.99.5
@lua NA
*/
bool initWithSpriteFrames(CCArray *pFrames,float delay = 0.0f);
/** Initializes a CCAnimation with CCAnimationFrame
@since v2.0
@lua NA
*/
bool initWithAnimationFrames(CCArray* arrayOfAnimationFrames,unsigned int loops);
/**
* @js NA
* @lua NA
*/
virtual CCObject* copyWithZone(CCZone* pZone);
/** total Delay units of the CCAnimation. */
CC_SYNTHESIZE_READONLY(float,m_fTotalDelayUnits,TotalDelayUnits)
/** Delay in seconds of the "delay unit" */
CC_SYNTHESIZE(float,m_fDelayPerUnit,DelayPerUnit)
/** duration in seconds of the whole animation. It is the result of totalDelayUnits * delayPerUnit */
CC_PROPERTY_READONLY(float,m_fDuration,Duration)
/** array of CCAnimationFrames */
CC_SYNTHESIZE_RETAIN(CCArray*,m_pFrames,Frames)
/** whether or not it shall restore the original frame when the animation finishes */
CC_SYNTHESIZE(bool,m_bRestoreOriginalFrame,RestoreOriginalFrame)
/** how many times the animation is going to loop. 0 means animation is not animated. 1,animation is executed one time,... */
CC_SYNTHESIZE(unsigned int,m_uLoops,Loops)
};

/** @brief Animates a sprite given the name of an Animation */
class CC_DLL CCAnimate : public CCActionInterval
{
public:
/**
* @js ctor
*/
CCAnimate();
/**
* @js NA
* @lua NA
*/
~CCAnimate();
/** initializes the action with an Animation and will restore the original frame when the animation is over */
bool initWithAnimation(CCAnimation *pAnimation);
/**
* @js NA
* @lua NA
*/
virtual CCObject* copyWithZone(CCZone* pZone);
virtual void startWithTarget(CCNode *pTarget);
virtual void stop(void);
virtual void update(float t);
virtual CCActionInterval* reverse(void);
public:
/** creates the action with an Animation and will restore the original frame when the animation is over */
static CCAnimate* create(CCAnimation *pAnimation);
CC_SYNTHESIZE_RETAIN(CCAnimation*,m_pAnimation,Animation)
protected:
std::vector<float>* m_pSplitTimes;
int m_nNextFrame;
CCSpriteFrame* m_pOrigFrame;
unsigned int m_uExecutedLoops;
};

看着有点迷茫呀!!理一理思路。
其实我们在运用的过程中遵循以下步骤就行了。
一、创建CCAnimation
1.通过CCAnimationCache创建

CCAnimationCache *animationcache = CCAnimationCache::sharedAnimationCache();
animationcache->addAnimationsWithFile("animations/animations-2.plist");
CCAnimation *animation = animationcache->animationByName("dance_1");//刚开始不知道这个参数是哪里来的,其实是plist里面的,你用记事本打开就可以看得到,它是一个动画标识
animation->setRestoreOriginalFrame(true);
2.通过CCAnimation的create***函数或是add***函数创建
用create***函数创建
CCArray *animations = CCArray::createWithCapacity(14);  
char str[100]={0};
for(int i = 1; i< 14; i++)
{
sprintf(str,”grossini_dance_%02d.png”,i); 
CCSpriteFrame *frame = frameCache->spriteFrameByName(str);  
animations->addobject(frame);  
}
CCAnimation* animation = CCAnimation::createWithSpriteFrames(animations,2.0f);
当然,你也可以通过add***函数创建
CCAnimation* animation = CCAnimation::create();
for( int i=1;i<15;i++)
 {
        char szName[100] = {0};
        sprintf(szName,"Images/grossini_dance_%02d.png",i);
        animation->addSpriteFrameWithFileName(szName); //加载动画的帧
 }
二、通过CCAnimation创建CCAnimate
这个就比较简单了,一般就是
CCAnimate *animate = CCAnimate::create(animation);
三、通过CCSprite的runaction绑定CCAnimate到精灵。
也比较简单
sprite->runAction(CCSequence::create(animate,animate->reverse(),NULL));

参考文章:

http://www.jb51.cc/article/p-yugmmlhp-bch.html

http://www.jb51.cc/article/p-bwlbigwy-bch.html

http://cocos2d.9tech.cn/news/2014/0303/39946.html

http://www.jb51.cc/article/p-ffbjhgci-bme.html

【cocos2dx之CCAnimation、CCAnimate、CCAnimationCache使用】的更多相关文章

  1. CSS中实现动画效果-附案例

    这篇文章主要介绍了 CSS中实现动画效果并附上案例代码及实现效果,就是CSS动画样式处理,动画声明需要使用@keyframes name,后面的name是人为定义的动画名称,下面我们来看看文章的具体实现内容吧,需要的小伙伴可以参考一下

  2. ios – 围绕其中心点旋转UIImageView?

    我在UIImageView中有一个透明的png,我想围绕它的中心点旋转.代码应该非常简单:图像以正确的速度/时间和直角旋转,但其位置会发生偏移.这是一个正在发生的事情的例子:灰色方块只是为了在屏幕上显示位置.透明的png是另一个图.白色虚线显示UIImageView的中心.图像的左侧显示图像的原始位置,右侧显示使用上述代码旋转后的图像.黑色和白色圆圈位于图像文件的中心.有什么东西我不见了吗?

  3. ios – 将UIView的框架和角半径合在一起

    码:此代码是UIView的扩展.解决方法我像这样调整我的圈子视图:

  4. ios – 如何在Cocos2D 3.x中为CCSprite制作动画?

    你知道如何在新的Cocos2Dv3.x中动画CCSprite吗?许多类都被改变了,旧的方法似乎不起作用.任何的想法?谢谢.额外信息解决方法这是它的工作原理:

  5. ios5 – UIPageViewController转换速度/持续时间?

    有没有办法改变页面卷曲过渡的默认持续时间?这是快速的方式然后我希望它会是?谢谢沙尼解决方法Hy,这是使用默认转换来卷曲页面和指定卷曲速度的方法.祝你工作顺利.

  6. ios – 重用UICollectionView中的单元格时,会短暂显示重用的UICollectionViewCell的旧内容

    我正在使用UICollectionView来显示从URL异步加载的图片网格.我的集合视图使用可重用单元格来显示UICollectionViewCells.当不重复使用单元格时,所有内容都会正确显示,但是当我滚动一下时,重用的单元格会在它们开始正常行为之前短暂闪烁旧内容.以下是自定义UICollectionViewController的实现:这是自定义UICollectionViewCell的实现.

  7. 在iOS中移动UICollectionView的单元格?

    我有一个UICollectionView.I我试图给它作为SpringBoard的功能.我有能力给每个单元格摇动动画.但是我想当图标摇动时,我应该能够移动他们.为了摇动单元格,我已经在每个单元格上添加了UILongPressGesture.当手势结束时,我已经添加了一个自定义动画.还在左上角添加了一个删除按钮.长按手势代码:添加手势到集合视图回调方法单元格在inde路径项在这里工作正常为了移动单元

  8. iOS:CAShapeLayer绘制非直视图像并为其形状设置动画

    我一直在阅读有关CAShapeLayer的文档,但我仍然不太明白.根据我的理解,Layer始终是扁平的,其大小始终是矩形.另一方面,CAShapeLayer允许您定义不仅仅是矩形的图层.它可以是圆形,三角形等,只要您将它与UIBezierPaths一起使用即可.我的理解是在这里吗?

  9. ios – CGContextSaveGState:应用启动时的无效上下文0x0

    FWW我的应用程序大多是用Swift2.0编写的,在iOS9上使用Xcode7b6.解决方法我遇到同样的问题,其原因是,我正在为不同的屏幕设置不同的状态栏样式.因此,我需要在plist中添加“查看基于控制器的状态栏外观”键.如果我删除此键,警告消失.

  10. ios – 可以将视图放在彼此之上

    我正在构建一个Watch应用程序,其中我要覆盖WKInterfaceImage与一组WKInterfaceLabel对象.在StoryBoard编辑器中似乎无法做到这一点.有没有人能够为WatchApp设计出相互之间的观点?

随机推荐

  1. 【cocos2d-x 3.x 学习笔记】对象内存管理

    Cocos2d-x的内存管理cocos2d-x中使用的是上面的引用计数来管理内存,但是又增加了一些自己的特色。cocos2d-x中通过Ref类来实现引用计数,所有需要实现内存自动回收的类都应该继承自Ref类。下面是Ref类的定义:在cocos2d-x中创建对象通常有两种方式:这两中方式的差异可以参见我另一篇博文“对象创建方式讨论”。在cocos2d-x中提倡使用第二种方式,为了避免误用第一种方式,一般将构造函数设为protected或private。参考资料:[1]cocos2d-x高级开发教程2.3节[

  2. 利用cocos2dx 3.2开发消灭星星六如何在cocos2dx中显示中文

    由于编码的不同,在cocos2dx中的Label控件中如果放入中文字,往往会出现乱码。为了方便使用,我把这个从文档中获取中文字的方法放在一个头文件里面Chinese.h这里的tex_vec是cocos2dx提供的一个保存文档内容的一个容器。这里给出ChineseWords,xml的格式再看看ChineseWord的实现Chinese.cpp就这样,以后在需要用到中文字的地方,就先include这个头文件然后调用ChineseWord函数,获取一串中文字符串。

  3. 利用cocos2dx 3.2开发消灭星星七关于星星的算法

    在前面,我们已经在GameLayer中利用随机数初始化了一个StarMatrix,如果还不知道怎么创建星星矩阵请回去看看而且我们也讲了整个游戏的触摸事件的派发了。

  4. cocos2dx3.x 新手打包APK注意事项!

    这个在编译的时候就可以发现了比较好弄这只是我遇到的,其他的以后遇到再补充吧。。。以前被这两个问题坑了好久

  5. 利用cocos2dx 3.2开发消灭星星八游戏的结束判断与数据控制

    如果你看完之前的,那么你基本已经拥有一个消灭星星游戏的雏形。开始把剩下的两两互不相连的星星消去。那么如何判断是GameOver还是进入下一关呢。。其实游戏数据贯穿整个游戏,包括星星消除的时候要加到获得分数上,消去剩下两两不相连的星星的时候的加分政策等,因此如果前面没有做这一块的,最好回去搞一搞。

  6. 利用cocos2dx 3.2开发消灭星星九为游戏添加一些特效

    needClear是一个flag,当游戏判断不能再继续后,这个flag变为true,开始消除剩下的星星clearSumTime是一个累加器ONE_CLEAR_TIME就是每颗星星消除的时间2.连击加分信息一般消除一次星星都会有连击信息和加多少分的信息。其实这些combo标签就是一张图片,也是通过控制其属性或者runAction来实现。源码ComboEffect.hComboEffect.cpp4.消除星星粒子效果消除星星时,为了实现星星爆裂散落的效果,使用了cocos2d提供的粒子特效引擎对于粒子特效不了

  7. 02 Cocos2D-x引擎win7环境搭建及创建项目

    官网有搭建的文章,直接转载记录。环境搭建:本文介绍如何搭建Cocos2d-x3.2版本的开发环境。项目创建:一、通过命令创建项目前面搭建好环境后,怎样创建自己的Cocos2d-x项目呢?先来看看Cocos2d-x3.2的目录吧这就是Cocos2d-x3.2的目录。输入cocosnew项目名–p包名–lcpp–d路径回车就创建成功了例如:成功后,找到这个项目打开proj.win32目录下的Hello.slnF5成功了。

  8. 利用cocos2dx 3.2开发消灭星星十为游戏添加音效项目源码分享

    一个游戏,声音也是非常的重要,其实cocos2dx里面的简单音效引擎的使用是非常简单的。我这里只不过是用一个类对所有的音效进行管理罢了。Audio.hAudio.cpp好了,本系列教程到此结束,第一次写教程如有不对请见谅或指教,谢谢大家。最后附上整个项目的源代码点击打开链接

  9. 03 Helloworld

    程序都有一个入口点,在C++就是main函数了,打开main.cpp,代码如下:123456789101112131415161718#include"main.h"#include"AppDelegate.h"#include"cocos2d.h"USING_NS_CC;intAPIENTRY_tWinMain{UNREFERENCED_ParaMETER;UNREFERENCED_ParaMETER;//createtheapplicationinstanceAppDelegateapp;return

  10. MenuItemImage*图标菜单创建注意事项

    学习cocos2dx,看的是cocos2d-x3.x手游开发实例详解,这本书错误一大把,本着探索求知勇于发现错误改正错误的精神,我跟着书上的例子一起调试,当学习到场景切换这个小节的时候,出了个错误,卡了我好几个小时。

返回
顶部