前面的文章中也有例子是创建一个动画,利用的是文件创建,将所有的Sprite加载到一个文件中去,然后读出,现在我们尝试不用文件,直接利用CCAnimate来创建一个sprite动画,例子代码如下:
void MyActionAnimateLayer::initLayer() {
CCSize size = CCDirector::sharedDirector()->getWinSize();
CCSprite *sprite = CCSprite::create("grossini.png");
this->addChild(sprite, 1);
sprite->setPosition(ccp(size.width / 2.0f, size.height / 2.0f));
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); //加载动画的帧
}
animation->setDelayPerUnit(2.8f / 14.0f);
animation->setRestoreOriginalFrame(true);
animation->setLoops(10); //动画循环10次
CCAnimate *animate = CCAnimate::create(animation);
sprite->runAction(CCSequence::create(animate, animate->reverse(), NULL));
}
创建完毕!大家可以看一下效果了!
如果想让你的动画无限次循环的话,你可以这样做:
sprite->runAction(CCRepeatForever::create(dynamic_cast<CCActionInterval *>(CCSequence::create(animate, animate->reverse(), NULL))));
当然也还有其他的方法了,这里我就不再举例子了!