两个静态接口,第一个是未打包方法,第二个是解析plist和png方法
AnimateTool.h
/*******************************************************************************************************************************/
//By Prpr_Saber
//2018年7月21日13:18:41
/******************************************************************************************************************************/
#ifndef _ANIMATETOOL_H_
#define _ANIMATETOOL_H_
#include "cocos2d.h"
USING_NS_CC;
class AnimateTool
{
public:
AnimateTool();
~AnimateTool();
示例:图片组为run01, run02, run03... run12, 不要超过20
//十位数则“run”+ “%02d”, 宽50, 高42, 帧数12张, 帧率0.05f即50毫秒, 指向类对象的指针this。
static Sprite* getSpriteAction(char* s, float width, float height, int n, float fps, Sprite* p);
//plist、png、帧数、帧率(0.05f)。
//*png格式“photo01.png”
static ActionInterval* getSpriteAction(std::string plist, std::string png, int n, float fps);
};
#endif
AnimateTool.cpp
#include "AnimateTool.h"
AnimateTool::AnimateTool()
{
}
AnimateTool::~AnimateTool()
{
}
Sprite * AnimateTool::getSpriteAction(char* s, float width, float height, int n, float fps, Sprite* p)
{
Vector pAniFrameVec = Vector(n);//用模板命名一个名为pAniFrameVec容量n的Vector
char szFile[20] = "";
Sprite *pSprite = nullptr;
for (int i = 1; i <= n; i++)
{
sprintf_s(szFile, s, i);
auto pFrame = SpriteFrame::create(szFile, Rect(0, 0, width, height));
if (i == 1)
{
pSprite = Sprite::createWithSpriteFrame(pFrame);
pSprite->setAnchorPoint(Vec2(0, 0));
pSprite->setPosition(Vec2(p->getContentSize().width / 2, p->getContentSize().height / 2));
p->addChild(pSprite, 1);
}
pAniFrameVec.pushBack(pFrame);
}
Animation *pAnimation = Animation::createWithSpriteFrames(pAniFrameVec, fps);
Animate* pAnimate = Animate::create(pAnimation);
ActionInterval* seq = (ActionInterval *)(Sequence::create(pAnimate, NULL));
pSprite->runAction(RepeatForever::create(seq));
return pSprite;
}
ActionInterval* AnimateTool::getSpriteAction(std::string plist, std::string png, int n, float fps)
{
std::string name = png;
for (size_t i = 1; i < name.length(); i++ )
{
if (name[i] == '.')
{
name = name.substr(0, i);
break;
}
}
SpriteFrameCache* sfc = SpriteFrameCache::getInstance();
sfc->addSpriteFramesWithFile(plist, png);
Vector pAniFrameVec = Vector(n);
std::stringstream ss;
std::string filename;
char num[50] = "";
for (int i = 1; i <= n; i++)
{
sprintf(num, "%02d", i);
ss.clear();
ss.str("");
ss << name << num << ".png";
filename = ss.str();
auto pFrame = sfc->getSpriteFrameByName(filename);
pAniFrameVec.pushBack(pFrame);
}
Animation *pAnimation = Animation::createWithSpriteFrames(pAniFrameVec, fps);
Animate* pAnimate = Animate::create(pAnimation);
ActionInterval* seq = (ActionInterval *)(Sequence::create(pAnimate, NULL));
return seq;
}