转载请标明:转载自【小枫栏目】,博文链接:http://blog.csdn.net/my183100521/article/details/10157713
一个遊戏的角色如果只是静止的, 相信不会太招人喜欢, 动作流畅自然的遊戏角色, 更能让遊戏大放异彩,这次让我们看一下在 cocos2d-x 上怎样处理角色的动画吧.
内容重点: 动画, CCAnimation, CCAnimate, CCSpriteFrameCache
简单过程是,使用CCTexture2D加载图片 ,用CCTexture2D生成对应的CCSpriteFrame(对应的就是帧),将CCSpriteFrame添加到CCAnimation生成动画数据,用CCAnimation生成CCAnimate(就是最终的动画动作),最后用CCSprite执行这个动作。
相关的类关系图
本人资源目录如下:
下载动画资源素材地址:http://www.66rpg.com/category.php?c=3
参考老G教程:http://4137613.blog.51cto.com/4127613/759610
HelloWorldScene.cpp的init()添加代码
// //#1:生成动画需要的数据类 CCTexture2D *texture = CCTextureCache::sharedTextureCache()->addImage("miku.png"); CCArray *animFrames = CCArray::create(); for (int i=0; i<4; i++) { CCSpriteFrame *frame=CCSpriteFrame::frameWithTexture(texture,CCRectMake(32*i, 48*3, 32, 48)); animFrames->addObject(frame); } CCAnimation *animation = CCAnimation::create(); animation->initWithSpriteFrames(animFrames,0.2f); animFrames->release(); //#2:初始化并设置Sprite CCSpriteFrame *frame0=CCSpriteFrame::frameWithTexture(texture,CCRectMake(32*0, 48*0, 32, 48)); CCSprite *sprite = CCSprite::spriteWithSpriteFrame(frame0); sprite->setPosition(ccp(size.width/2,size.height/2)); addChild(sprite); //#3:使用animation生成一个动画动作animate CCAnimate *animate = CCAnimate::actionWithAnimation(animation); sprite->runAction(CCRepeatForever::actionWithAction(animate));
效果图:
更改下代码,编写常用函数
CCAnimation *HelloWorld::animationWithStrip(const char *name,int count, float delay, int col, int row, int startingRow) { CCArray *animFrames = new CCArray[count]; CCSpriteFrameCache *cache = CCSpriteFrameCache::sharedSpriteFrameCache(); CCSpriteFrame *strip = cache->spriteFrameByName(name); CCTexture2D *texture = strip->getTexture(); CCSize originalSize = strip->getOriginalSizeInPixels(); bool rotated = strip->isRotated(); CCRect stripRect = strip->getRect(); float w = originalSize.width/col; float h = originalSize.height/row; float x = stripRect.origin.x; float y = stripRect.origin.y; int n = 0; bool done = false; float xx; float yy = y; for (int i=startingRow;i<row && !done;i++) { if (rotated) xx = (x+originalSize.height)-(i+1)*h; else xx = x; for (int j=0;j<col && !done;j++) { CCSpriteFrame *frame = CCSpriteFrame::frameWithTexture(texture, CCRectMake(xx, yy, w, h)); frame->setRotated(rotated); animFrames->addObject(frame); if (rotated) { yy += w; } else xx += w; n++; if (n >= count) done = true; } if (rotated) yy = y; else yy += h; } CCAnimation *animation = CCAnimation::create(); animation->initWithSpriteFrames(animFrames,delay); return animation; }
测试代码
CCAnimation *animation = animationWithStrip("miku.png",4, 0.1f, 4, 4, 3); CCSprite *pSprite = CCSprite::spriteWithSpriteFrameName("miku1.png"); pSprite->setPosition(ccp(size.width/2,size.height/2)); pSprite->runAction(CCRepeatForever::actionWithAction(CCAnimate::actionWithAnimation(animation))); this->addChild(pSprite);
效果图
CCAnimation *HelloWorld::animationWithSingleFrames(const char *name, int count, float delay) { CCSpriteFrameCache *cache = CCSpriteFrameCache::sharedSpriteFrameCache(); CCArray *animFrames = CCArray::create(); char str[80]; for (int k = 0; k< count; k++){ sprintf(str, "%s%d.png",name,(k+1)); CCSpriteFrame *frame = cache->spriteFrameByName(str); animFrames->addObject(frame); } CCAnimation *animation = CCAnimation::create(); animation->initWithSpriteFrames(animFrames,delay); return animation; }
测试代码:
CCAnimation *animation = animationWithSingleFrames("miku",4, 0.1f); CCSprite *pSprite = CCSprite::spriteWithSpriteFrameName("miku1.png"); pSprite->setPosition(ccp(size.width/2,size.height/2)); pSprite->runAction(CCRepeatForever::actionWithAction(CCAnimate::actionWithAnimation(animation))); this->addChild(pSprite);效果图
博文就写了这里,若有什么地方不好,望各大网友们,留言下,大家互相交流~~~
引用原帖:http://www.cocoachina.com/gamedev/misc/2012/0528/4297.html