之前都是使用TP格式化swf来创建动画,今天遇到了个问题,我拿到的动画序列图是合成好的一张png。没有plist文件。悲催...
现在只好自己画区域创建动画。
先看看png序列图。我就随便拿一个来用用。(先说明哈,该图是我从一个android游戏里面抽出来的。无侵权之意。)
CCTexture2D *playerRunTexture = CCTextureCache::sharedTextureCache()->addImage("player_run.png");
CCMutableArray<CCSpriteFrame*>* animFrames = new CCMutableArray<CCSpriteFrame*>(11);
for(int i=0;i<8;i++){
animFrames->addObject(CCSpriteFrame::frameWithTexture(playerRunTexture, cocos2d::CCRectMake(72*i, 0, 72, 72)));
}
//生成CCAnimation对象
CCAnimation* animation = new CCAnimation();
animation->initWithFrames(animFrames, 0.08f);
animFrames->release();
//创建动画
CCAnimate *animate = CCAnimate::actionWithAnimation(animation, false);
CCSprite* player=CCSprite::spriteWithSpriteFrame(CCSpriteFrame::frameWithTexture(playerRunTexture, cocos2d::CCRectMake(0, 0, 72, 72)));
player->runAction(CCRepeatForever::actionWithAction(animate));
player->setPosition(ccp(100,mWinSize.height*.5-45));
this->addChild(player,zOrder++);
OK,就这样咯,看看咱的效果:
2012-12-07 更新:
今天看到cocos2d-x 2.0.1的更改很了多函数,这个方法可能已经不能用了,我重新写了一个方法
#define FRAMETIME 0.08f CCActionInterval* AnimateManager::createRFAnimFormPng(const char* pngName,CCSize frameSize,int frames){ CCTexture2D *playerRunTexture = CCTextureCache::sharedTextureCache()->addImage(pngName); CCAnimation* animation = CCAnimation::create(); for( int i = 0;i < frames;i++){ animation->addSpriteFrame(CCSpriteFrame::create(playerRunTexture, cocos2d::CCRectMake(frameSize.width*i, 0, frameSize.width, frameSize.height))); } // should last 2.8 seconds. And there are 14 frames. animation->setDelayPerUnit(FRAMETIME); CCAnimate* action = CCAnimate::create(animation); return CCRepeatForever::create(action); }
每一帧的时间
#define FRAMETIME 0.08f
png的图片名称
const char* pngName
这个序列图片中一个图的宽和高
CCSize frameSize
这个序列图片有多少帧
int frames