Cocos2d-x 动画(Animate)的制作

来源:互联网 发布:stm32串口接收数据 编辑:程序博客网 时间:2024/05/01 17:02

当我们拿到一张一张的动画图片后,首先将动画图片用TexturePackerGUI做出一个*.plist文件,接下来就可以开始动手了。

先讲一下动画制作的流程

1.将我们一个一个的动画图片创建为CCSpriteFrame, 也就是做成精灵帧

2.再将创建好的CCSpriteFrame加到一个CCAnimation中, 也就是将精灵帧进行打包animation->addSpriteFrame(fram1); 。。。

3.设置动画帧之间的间隔和循环次数

animation->setDelayPerUnit(0.1f);//设置帧与帧之间的间隔

animation->setLoops(kCCRepeatForever);//执行次数

4.将我们打好的包创建(create())成动画CCAnimate

5.此刻把动画CCAnimate当做一个精灵来runAction。

代码示例:

创建一个精灵,方便后面使用

CCSprite* spr = CCSprite::create();spr->setPosition(ccp(240, 160));addChild(spr);

1,傻瓜式版本,但是好理解

//创建精灵帧CCSpriteFrame* fram1 = CCSpriteFrame::create("animation/p_2_01.png", CCRectMake(0, 0, 80, 80));CCSpriteFrame* fram2 = CCSpriteFrame::create("animation/p_2_02.png", CCRectMake(0, 0, 80, 80));CCSpriteFrame* fram3 = CCSpriteFrame::create("animation/p_2_03.png", CCRectMake(0, 0, 80, 80));CCSpriteFrame* fram4 = CCSpriteFrame::create("animation/p_2_04.png", CCRectMake(0, 0, 80, 80));
//将动画帧打包        CCAnimation* animation = CCAnimation::create();   animation->addSpriteFrame(fram1);animation->addSpriteFrame(fram2);animation->addSpriteFrame(fram3);animation->addSpriteFrame(fram4);</span>
<span style="font-size:14px;">animation->setDelayPerUnit(0.1f);//设置帧与帧之间的间隔animation->setLoops(kCCRepeatForever);//执行次数
<span style="font-size:14px;"><span style="white-space:pre"></span>CCAnimate* animate = CCAnimate::create(animation);<span style="white-space:pre"></span>spr->runAction(animate);
够直观了吧。接下来我们将上述的步骤做的简洁一些,也就是我们实际开发中能用的

CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("animation/plan.plist");CCAnimation* animation = CCAnimation::create();char nameBuf[100];for (int i = 0; i < 8; i++){memset(nameBuf, 0, sizeof(nameBuf));sprintf(nameBuf, "p_2_0%d.png", i + 1);animation->addSpriteFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(nameBuf));}animation->setDelayPerUnit(0.1f);animation->setLoops(kCCRepeatForever);CCAnimate* animate = CCAnimate::create(animation);spr->runAction(animate);</span>
还可以再简洁一些
CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("animation/plan.plist");CCAnimation* animation = CCAnimation::create();char nameBuf[100];for (int i = 1; i < 9; i++){memset(nameBuf, 0, sizeof(nameBuf));sprintf(nameBuf, "p_2_0%d.png", i);animation->addSpriteFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(nameBuf));}animation->setDelayPerUnit(0.1f);animation->setLoops(kCCRepeatForever);CCAnimate* animate = CCAnimate::create(animation);spr->runAction(animate);
参考文章:http://www.jellythink.com/archives/752


0 1
原创粉丝点击