【cocos2d-x 手游研发小技巧(17)封装动画插件,序列帧 plist+png】

前端之家收集整理的这篇文章主要介绍了【cocos2d-x 手游研发小技巧(17)封装动画插件,序列帧 plist+png】前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

今天是2014年的最后一天 明天就是2015年了 今天给大家一点干活

我就直接上代码


#ifndef __ENTITY_ACTION_H__
#define __ENTITY_ACTION_H__
#include "cocos2d.h"
#include "cocos-ext.h"
#include "game/entity/UISprite.h"

class EntityAction : public CCNode
{
public:
EntityAction(void);
~EntityAction(void);
virtual void update(F32 dt);//定时器
void PlistDownload(const C8* Plist,const C8 *Image,const C8 *SpriteName,float SequenceFrameTime = 0.2f,float SpritePositionX = 0.0f,float SpritePositionY = 0.0f);//第一个是plist 第二个 Image 第三个 int con 是数量
void RequestOnePlist(const char* url);
void RequestOneImage(const char* url);
void onPlistCompleted(cocos2d::CCNode* sender,void* data);
void onImageCompleted(cocos2d::CCNode* sender,void* data);
void WriteZipFileGeneralPlist( const char* pData,int fileSize,const C8* zipPath);
void WriteZipFileGeneralImage( const char* pData,const C8* zipPath);
void SequenceFrame();


private:
int nDownloadNumber;
stringc m_sPlistName;//plist的路劲
stringc m_sImageName;//Image的图片
stringc m_sSpriteName;
int m_nDownloadNum; //一个有多少
float m_fSpritePositionX;//x的位置
float m_fSpritePositionY;//y的位置
float m_fSequenceFrameTime;//时间

cocos2d::CCSpriteBatchNode *_actors;
cocos2d::CCSprite *_bear;
};
#endif

.cpp

#include "stdafx.h"
#include "EntityAction.h"
#include "network/jsonMsg/jsonMsgManager.h"
#include "cocos-ext.h"
#include "network/HttpRequest.h"
#include "io/io.h"
#include "game/FileManager.h"
#include "game/ai/timeManager.h"
#include "game/gamestate/GameStatePlay.h"

USING_NS_CC_EXT;
static HttpAdapter* httpAdapter;
static bool m_bPlistFlag;//Plist 是否下載完成
static bool m_bPngFlag;//PNG是否下載完成



EntityAction::EntityAction(void)
{
m_bPlistFlag = false;
m_bPngFlag = false;
m_fSpritePositionX = 0.0f;
m_fSpritePositionY = 0.0f;
m_fSequenceFrameTime = 0.0f;


}

EntityAction::~EntityAction(void)
{
unscheduleUpdate();
CCDirector::sharedDirector()->resume();
this->removeFromParentAndCleanup(true);
}

void EntityAction::update(F32 dt)//控制时间
{
//m_bPlistFlag
//CCLOG("%d",m_bPlistFlag);
if (m_bPlistFlag == true && m_bPngFlag == true)
{
m_bPlistFlag = false;
m_bPngFlag = false;
SequenceFrame();
}

}


//位置 多长
void EntityAction::SequenceFrame()
{
CCSpriteFrameCache *frameCache = CCSpriteFrameCache::sharedSpriteFrameCache();
frameCache->addSpriteFramesWithFile(m_sPlistName.c_str());
int index = 1;
CCArray* pAnimFrames = CCArray::create();
CCSpriteFrame *pFrame = NULL;
do
{
char str[100] = {0};
sprintf(str,"%s_%d.png",m_sSpriteName.c_str(),index);
pFrame = frameCache->spriteFrameByName(str);
if (pFrame == NULL)
{
break;
}
pAnimFrames->addObject(pFrame);
index++;
} while (true);

CCAnimation *pAnimation = CCAnimation::createWithSpriteFrames(pAnimFrames,m_fSequenceFrameTime);
CCAnimate *pAnim = CCAnimate::create(pAnimation);

CCSprite *pGrossini = CCSprite::create();
pGrossini->setPosition(ccp(m_fSpritePositionX,m_fSpritePositionY));
this->addChild(pGrossini);
pGrossini->runAction(CCRepeatForever::create(pAnim));
}


void EntityAction::PlistDownload(const C8* Plist,float SequenceFrameTime,float SpritePositionX,float SpritePositionY) //plist下载 这个是测试
{

m_sPlistName = Plist;
m_sImageName = Image;
m_sSpriteName = SpriteName;
m_fSpritePositionX = SpritePositionX;
m_fSpritePositionY = SpritePositionY;
m_fSequenceFrameTime = SequenceFrameTime;

//如果传过来的url为空 直接返回
if (strcmp(Plist,"") == 0 && strcmp(Image,"") == 0)
{
return ;
}
//判断是否为网络图片
if (strlen(Plist) > 4 && Plist[0] == 'h' && Plist[1] == 't' && Plist[2] == 't' && Plist[3] == 'p' && strlen(Image) > 4 && Image[0] == 'h' && Image[1] == 't' && Image[2] == 't' && Image[3] == 'p')
{
m_sPlistName = GameIns.GetJsonMsgMgr()->GetFullPictureNameByUrl(Plist);
//这个是返回文件路劲 大家一般不需要 可以不要

m_sImageName = GameIns.GetJsonMsgMgr()->GetFullPictureNameByUrl(Image);
}
CCSprite *m_pSprite = cocos2d::CCSprite::spriteWithFile(m_sImageName.c_str());
if (m_pSprite == NULL)
{
RequestOnePlist(Plist);//下载plist
RequestOneImage(Image);//下载图片
}
else
{
m_pSprite->removeFromParentAndCleanup(true);
m_bPlistFlag = true;
m_bPngFlag = true;
}



CCDirector::sharedDirector()->getScheduler()->scheduleUpdateForTarget(this,false);
}

//下載 plist
void EntityAction::RequestOnePlist(const char* url)
{
if (httpAdapter == NULL)
{
httpAdapter = SYTX_NEW HttpAdapter();//SYTX_NEW new
}
CCHttpRequest *request = SYTX_NEW CCHttpRequest();
request->setUrl(url);
request->setRequestType(CCHttpRequest::kHttpGet);
request->setResponseCallback(httpAdapter,callfuncND_selector(EntityAction::onPlistCompleted));
request->setTag(url);
CCHttpClient::getInstance()->send(request);
request->release();
}
//下載 jpg
void EntityAction::RequestOneImage(const char* url)
{
if (httpAdapter == NULL)
{
httpAdapter = SYTX_NEW HttpAdapter();
}
CCHttpRequest *request = SYTX_NEW CCHttpRequest();
request->setUrl(url);
request->setRequestType(CCHttpRequest::kHttpGet);
request->setResponseCallback(httpAdapter,callfuncND_selector(EntityAction::onImageCompleted));
request->setTag(url);
CCHttpClient::getInstance()->send(request);
request->release();
}

//CCHttpRequest 回调方法
void EntityAction::onPlistCompleted(cocos2d::CCNode* sender,void* data)
{
stringc pszZipFilePath = cocos2d::CCFileUtils::sharedFileUtils()->getWriteablePath() + "ui_load";//路劲
cocos2d::CCFileUtils::sharedFileUtils()->createDirectory(pszZipFilePath.c_str());
CCHttpResponse *response = (CCHttpResponse*)data;
if (!response)
{
return;
}
char* szUrl;
stringc strImageName;
char szText[2048] = {0};
SPRINTF(szText,"%s",response->getHttpRequest()->getTag());
szUrl = szText;
strImageName = GameIns.GetJsonMsgMgr()->GetFullPictureNameByUrl(szUrl);
if (0 != strlen(response->getHttpRequest()->getTag()))
{
DBGPRINTLN("ui: %s image download successfully",response->getHttpRequest()->getTag());
}
S32 statusCode = response->getResponseCode();
C8 statusString[2048] = {};
SPRINTF(statusString,"HTTP Status Code: %d,tag = %s",statusCode,response->getHttpRequest()->getTag());
DBGPRINTLN("response code: %d",statusCode);
const std::vector<C8> *buffer = response->getResponseData();
WriteZipFileGeneralPlist(&(*buffer)[0],buffer->size(),strImageName.c_str());

stringc UrlAddress =szUrl;//url 地址
if (UrlAddress.find_last_of("#") != -1)
{
stringc versionNumber=UrlAddress.substr(UrlAddress.find_last_of("#")+1);//版本号
GameIns.GetFileManager()->InsertOnePic(strImageName.c_str(),GameIns.GetTimeMgr()->GetDWLocalTime(),versionNumber.c_str());
}
else
{
GameIns.GetFileManager()->InsertOnePic(strImageName.c_str(),"0");
}
m_bPlistFlag = true;
CCLOG("----%d",m_bPlistFlag);

}

////CCHttpRequest 回调方法
void EntityAction::onImageCompleted(cocos2d::CCNode* sender,statusCode);
const std::vector<C8> *buffer = response->getResponseData();
WriteZipFileGeneralImage (&(*buffer)[0],"0");
}
m_bPngFlag = true;
CCLOG("----%d",m_bPngFlag);

}
//写plist图片
void EntityAction::WriteZipFileGeneralPlist( const char* pData,const C8* zipPath)
{
stringc pszZipFilePath;

FILE* File = NULL;
#if ANDROID
pszZipFilePath = zipPath;
File = fopen(pszZipFilePath.c_str(),"wb+");
#elif(CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
pszZipFilePath = zipPath;
File = fopen(pszZipFilePath.c_str(),"wb+");
#else
pszZipFilePath = cocos2d::CCFileUtils::sharedFileUtils()->getWriteablePath() + zipPath;
IO_FOPEN(File,pszZipFilePath.c_str(),"wb+");
#endif
//ADD_FLOAT_MSG(pszZipFilePath.c_str());
if (File)
{
fwrite(pData,sizeof(U8),fileSize,File);
fflush(File);
fclose(File);
}
m_bPlistFlag = true;
CCLOG("--WriteZipFileGeneralPlist--%d",m_bPlistFlag);


}

//写Image图片
void EntityAction::WriteZipFileGeneralImage( const char* pData,File);
fflush(File);
fclose(File);
}
m_bPngFlag = true;
CCLOG("--WriteZipFileGeneralImage--%d",m_bPngFlag);
}

代码下载

http://www.sundaboke.com/text/EntityAction.rar

猜你在找的Cocos2d-x相关文章