qinning199原创,欢迎转载,转载请注明:http://www.cocos2dx.net/?p=22
一、帧动画
你可以通过一系列图片文件,像如下这样,创建一个动画: CCAnimation *animation = CCAnimation::create();
//从本地文件系统中加载图片文件到CCSpriteFrame中区,然后添加到CCAnimation中
for (int i = 1; i < 15; i++)
{
char szImageFileName[128] = {0};
sprintf(szImageFileName, "Images/grossini_dance_%02d.png", i);
animation->addSpriteFrameWithFileName(szImageFileName);
}
animation->setDelayPerUnit(2.8f / 14.0f); // 这个动画包含14帧,将会持续2.8秒.
animation->setRestoreOriginalFrame(true); // 14帧播放完之后返回到第一帧
CCAnimate *action = CCAnimate::create(animation);
sprite->runAction(action); // 运行精灵对象
CCSpriteBatchNode* spritebatch = CCSpriteBatchNode::create("animations/grossini.png");
CCSpriteFrameCache* cache = CCSpriteFrameCache::sharedSpriteFrameCache();
cache->addSpriteFramesWithFile("animations/grossini.plist");
m_pSprite1 = CCSprite::createWithSpriteFrameName("grossini_dance_01.png");
spritebatch->addChild(m_pSprite1);
addChild(spritebatch);
CCArray* animFrames = CCArray::createWithCapacity(15);
char str[100] = {0};
for(int i = 1; i < 15; i++)
{
sprintf(str, "grossini_dance_%02d.png", i);
CCSpriteFrame* frame = cache->spriteFrameByName( str );
animFrames->addObject(frame);
}
CCAnimation* animation = CCAnimation::createWithSpriteFrames(animFrames, 0.3f);
m_pSprite1->runAction( CCRepeatForever::create( CCAnimate::create(animation) ) );
CCAnimationCache *cache = CCAnimationCache::sharedAnimationCache(); // 缓存在cocos2dx中一直是单例模式
cache->addAnimationsWithFile("animations/animations-2.plist");
CCAnimation animation = cache->animationByName("dance_1");
CCAnimate animate = CCAnimate::create(animation);
sprite->runAction(animate);