include “cocos2d.h”
USING_NS_CC;
class ActionTool
{
public:
//声明
//不需要知道图片数量 iloops:循环次数;delay:时间间隔
static Animate * animationWitheFrameName(const char *frameName,int iloops,float delay);
//知道图片数量 num:图片数量 ;
static Animate * animationWitheFameAndNum(const char *frameName,int num, float delay);
};
//定义
#include "ActionTool.h"
Antimate*ActionTool:animationWithFrameName(cosnt char *each_name,int iloops,float delay)
{
SpriteFrame 8frame=NULL;
Aniameion * animation =Animation::create();
int index=1;
do
{
String *name =String::createWithFormat("%s%d",each_name,index++);
frame =SpriteFrameCache::getinstance()->getSpriteFrameByName(name->getCString());
if(fame=NULL)
{
break;
}
animation->addSpriteFrame(frame);
}while(true);
animation->setDelayPerUnit(delay);
animation->setRestoreOriginalFrame(true);
Animate * animate=Animate::create(animation);
return animate;
}
//知道图片数量
Animate * animationWitheFameAndNum(const char *frameName,int num, float delay)
{
SpriteFrame 8frame=NULL;
Aniameion * animation =Animation::create();
for(int index=1;index<num;num++)
{
String *name =String::createWithFormat("%s%d",frameName,index++);
frame =SpriteFrameCache::getinstance()->getSpriteFrameByName(name->getCString());
}
animation->setDelayPerUnit(delay);
animation->setRestoreOriginalFrame(true);
Animate * animate=Animate::create(animation);
return animate;
//方法二
/*Vector<SpriteFrame*>animFrames;
char str20[];
for(int k=1;k<framecount;k++)
{
sprintf(str,"%s%d",frameName,k);
SpriteFrame *frame =SpriteFrameCache::getInstance()->spriteFrameByName(str);
animFrames.pushBack(frame);}
return Animate::create(Animation::crateWithSpriteFrames(animaFrames,delay));*/
return aniamte;
}
原文:http://www.cnblogs.com/feizuzu/p/f2f02299ae100c52b95d9f2e8136a900.html