cocos2d-x基本知识点(7):动画
来源:互联网 发布:c语言预处理 编辑:程序博客网 时间:2024/05/29 18:34
动画类
动画类CCAnimate也是一种动作,要实现CCAnimate,还需要定义CCAnimation等类
CCAnimationCache类是一个单例,用于缓存所有的动画和动画帧,使用如下:
CCAnimationCache::sharedAnimationCache()->addAnimation(animation,"dance");
CCAnimationCache* animCache = CCAnimationCache::sharedAnimationCache();
CCAnimation* normal = animCache->animationByName("dance");
CCAnimationFrame的使用:
CCSpriteFrame* frame = (CCSpriteFrame*)pObj;
CCAnimationFrame* animFrame = new CCAnimationFrame();
animFrame->initWithSpriteFrame(frame,1,NULL);//精灵帧,该动画帧延迟时间,CCDictionary类型的信息
CCAnimation就是动画,存储一个动画动作需要的所有帧,可以通过帧的数组定义
CCArray* animFrames = CCArray::create();
char tmp[50];
for(int j = 0;j <14; ++i){
sprintf(tmp,"grossini_dance_%02d.png",j+1);
CCSpriteFrame* frame = cache->spriteFrameByName(tmp);
animFrames->addObject(frame);
CCAnimation* animation = CCAnimation::create(animFrame,0.3f);
CCAnimate 动画动作就是一个动作类,可以通过CCAnimation动画来定义CCAnimate动画动作:
sprite->runAction(CCRepeateForever::create(CCAnimate::create(animation)));
cocos2d-x 中的动画使用实例:
AnimationCache::AnimationCache(){
CCSpriteFrameCache* frameCache = CCSpriteFrameCache::sharedSpriteFrameCache();
frameCache->addSpriteFrameWithFile("animations/grossini.plist");
frameCache->addSpriteFrameWithFile("animations/grossini_gray.plist");
frameCache->addSpriteFrameWithFile("animations/grossini_blue.plist");
CCArray* animaFrames = CCArray::create(15);
char str[100] = {0};
for(int i = 1;i <15;++i){
sprintf(str,"grossini_dance_%02d.png",i);
CCSpriteFrame* frame = frameCache->spriteFrameByName(str);
animFrames->addObject(frame);
}
CCAnimation* animation = CCAnimation::create(animFrames,0.2f);
CCAnimationCache::sharedAnimationCache()->addAnimation(animation,"dance");
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::create(animFrames,0.2f);
CCAnimationCache::sharedAnimationCache()->addAnimation(animation,"dance_gray");
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::create(animFrames,0.2f);
CCAnimationCache::sharedAnimationCache()->addAnimation(animation,"dance_blue");
CCAnimationCache* animCache = CCAnimationCache::sharedAnimationCache();
CCAnimation* normal = animCache->animationByName("dance");
normal->setRestoreOriginalFrame(true); // 动画播放完毕后回到初始帧
CCAnimation* dance_grey = animCache->animationByName("dance_grey");
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);
CCFiniteTimeAction seq = CCSequence::create(animN,animG,animB,NULL);
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);
grossini->runAction(seq);
}
通过plist文件定义动画:
CCSpriteFrameCache* frameCache = CCSpriteFrameCache::sharedSpriteFrameCache();
frameCache->addSpriteFramesWithFile("animations/grossini.plist");
frameCache->addSpriteFramesWithFile("animations/grossini_gray.plist");
frameCache->addSpriteFramesWithFile("animations/grossini_blue.plist");
CCAnimationCache::purgeSharedAnimationCache();
CCAnimationCache* animCache = CCAnimationCache::sharedAnimationCache();
animCache->addAnimationsWithFile("animations/animations.plist");
- cocos2d-x基本知识点(7):动画
- cocos2d-x基本知识点(1)
- cocos2d-x基本知识点(2)
- cocos2d-x基本知识点(3)
- cocos2d-x基本知识点(4)
- cocos2d-x基本知识点(5)
- cocos2d-x基本知识点:声音
- cocos2d-x基本知识点(8):菜单项
- cocos2d-x基本知识点:游戏存档
- cocos2d-x基本知识点:网络连接libcurl
- cocos2d-x基本知识点:粒子系统
- cocos2d-x基本知识点:文本渲染
- cocos2d-x基本知识点:按键事件
- cocos2d-x基本知识点(6)---动作系列(上)
- cocos2d-x基本知识点(6)---动作系列(中)
- cocos2d-x基本知识点(6)---动作系列(下)
- cocos2d-x基本知识点(6)---动作系列(end)
- cocos2d-x入门知识点
- 文章中获取第一张图片作为文章缩略图
- struts 和servlet的关系
- debug : use _ASSERT_EXPR
- TabHost的使用
- cocos2d-x 如何管理游戏资源
- cocos2d-x基本知识点(7):动画
- Linux命令:cp 命令--创建文件和目录的副本 mv命令--移动或重命名文件/目录
- C++关键字
- Sqoop2 Intermediate representation
- Android 上传图片
- 昆仑叶哲华,你真是让我摸不着头脑
- Android高效加载大图、多图解决方案,有效避免程序OOM
- android项目 之 来电管家(3) ----- 添加与删除黑名单
- linux进程间通信的几种机制的比较及适用场合