cocos2d-x -------之笔记篇 动画的实现
2014-07-20 18:50
260 查看
cocos2d-x 动画的实现
一、实现原理
动画的实现其实就是使用一个完整的动作图片集来实现动画,达到动态的效果
动画动作类(CCAnimate)是加载一个动画类来实现动作。
动画类(CCAnimation)加载一个精灵帧数组来构成一个动画,
CCAnimate函数:
static CCAnimate* create(CCanimation* pAnimation)
CCAnimation创建函数
CCAnimation* CCAnimation::createWithSpriteFrames(CCArray *frames, float delay/* = 0.0f*/)
参数:数组,间隔时间
二、动作实现步骤
1、首先需要加载一个纹理图片
CCTexture2D *texture = CCTextureCache::sharedTextureCache()->addImage(“纹理图片");
然后能够得到该纹理的宽高信息,
2、创建一个精灵,一般都是使用该纹理图片中的某个图片来作为显示精灵,这样在执行动画动作的时候能够更加的协调
首先我们需要加载
使用上得到的纹理创建一个精灵
CCSpriteFrame* spriteFrame1 = CCSpriteFrame::createWithTextrue(text,CCRectmake(x,y,宽,高));
CCSprite* sprite = CCSprite::createWithSpriteFrame(spriteFrame1);
这样能够将纹理的某个小区域用来创建一个精灵
3、创建一个精灵帧数组
CCArray array = CCArray::create();
然后将要执行动作的图片集存放在该数组中
4、使用该数组创建一个动画类
CCAnimation* animation = CCAnimation::createWithSpriteFrames(array,0.1f);
5、创建动画动作
CCAnimate* animate = CCAnimate::create(animation);
最后精灵执行该动作
下面是自己测试的代码:
一、实现原理
动画的实现其实就是使用一个完整的动作图片集来实现动画,达到动态的效果
动画动作类(CCAnimate)是加载一个动画类来实现动作。
动画类(CCAnimation)加载一个精灵帧数组来构成一个动画,
CCAnimate函数:
static CCAnimate* create(CCanimation* pAnimation)
CCAnimation创建函数
CCAnimation* CCAnimation::createWithSpriteFrames(CCArray *frames, float delay/* = 0.0f*/)
参数:数组,间隔时间
二、动作实现步骤
1、首先需要加载一个纹理图片
CCTexture2D *texture = CCTextureCache::sharedTextureCache()->addImage(“纹理图片");
然后能够得到该纹理的宽高信息,
2、创建一个精灵,一般都是使用该纹理图片中的某个图片来作为显示精灵,这样在执行动画动作的时候能够更加的协调
首先我们需要加载
使用上得到的纹理创建一个精灵
CCSpriteFrame* spriteFrame1 = CCSpriteFrame::createWithTextrue(text,CCRectmake(x,y,宽,高));
CCSprite* sprite = CCSprite::createWithSpriteFrame(spriteFrame1);
这样能够将纹理的某个小区域用来创建一个精灵
3、创建一个精灵帧数组
CCArray array = CCArray::create();
然后将要执行动作的图片集存放在该数组中
4、使用该数组创建一个动画类
CCAnimation* animation = CCAnimation::createWithSpriteFrames(array,0.1f);
5、创建动画动作
CCAnimate* animate = CCAnimate::create(animation);
最后精灵执行该动作
下面是自己测试的代码:
bool MyActiondonghua::init(){ if(!CCLayer::init()) return false; CCSize size = CCDirector::sharedDirector()->getWinSize(); //1 CCTexture2D * texture = CCTextureCache::sharedTextureCache()->addImage("dongzuo.png"); float texturewidht = texture->getContentSize().width; float textureheight = texture->getContentSize().height; float pwidth = texturewidht /10; float pheight = textureheight / 4; //2 //得到纹理图片的第一个图片 CCSpriteFrame* spriteFrame = CCSpriteFrame::createWithTexture(texture,CCRectMake(0,0,pwidth,pheight)); //使用这个spriteFrame来创建一个精灵进行显示 CCSprite* sprite = CCSprite::createWithSpriteFrame(spriteFrame); sprite->setPosition(ccp(size.width/2,size.height/2) ); sprite->retain(); this->addChild(sprite); //3 //创建动画 CCArray* array = CCArray::create(); for(int i = 0; i <9 ; i++){ CCSpriteFrame* tmpspritefreme = CCSpriteFrame::createWithTexture(texture,CCRectMake(pwidth*i,pheight,pwidth,pheight)); array->addObject(tmpspritefreme); } //4 CCAnimation *animation = CCAnimation::createWithSpriteFrames(array,0.1f); //5 CCAnimate* animate = CCAnimate::create(animation); //6 sprite->runAction(CCRepeatForever::create(animate)); return true; }
相关文章推荐
- cocos2d-x学习笔记(二)序列帧动画实现的几种方法
- Android游戏开发学习笔记(一):tweened animation自定义动画的实现
- cocos2d-x 使用action实现各种动画效果
- 【Visual C++】游戏开发笔记之十 基础动画显示(三) 透明动画的实现
- 【Visual C++】游戏开发笔记之十 基础动画显示(三) 透明动画的实现
- 【Visual C++】游戏开发笔记之十 基础动画显示(三) 透明动画的实现
- D3D学习笔记之六---简单的动画实现。
- cocos2d-x学习笔记20:切片动画库编写要点
- 【Visual C++】游戏开发笔记之十 基础动画显示(三) 透明动画的实现
- 【iOS-Cocos2d游戏开发之二十二 】CCSpeed实现CCAnimate动画进行时设置慢动作以及设置游戏加减速进行(塔防游戏必备)!
- 【Visual C++】游戏开发笔记之十 基础动画显示(三) 透明动画的实现
- cocos2d-x帧动画实现(续)
- 【iOS-Cocos2d游戏开发之二十二 】CCSpeed实现CCAnimate动画进行时设置慢动作以及设置游戏加减速进行(塔防游戏必备)!
- 【iphone游戏开发】iphone-Cocos2d游戏开发之四:精灵实现缩放,旋转,跳动,移动等动画
- [原]【Visual C++】游戏开发笔记之十 基础动画显示(三) 透明动画的实现
- cocos2d-x 使用action实现各种动画效果
- cocos2d-x帧动画实现(续)
- 【iOS-Cocos2d游戏开发之二十二 】CCSpeed实现CCAnimate动画进行时设置慢动作以及设置游戏加减速进行(塔防游戏必备)! .
- 【Visual C++】游戏开发笔记之十 基础动画显示(三) 透明动画的实现
- 游戏开发笔记之十 基础动画显示(三) 透明动画的实现