#include "json/rapidjson.h"
#include "json/Document.h"
#include "json/reader.h"
#include "json/writer.h"
#include "cocos2d.h"
USING_NS_CC;
bool SpaceShip::init(ValueMap&vm,int index){
do{
/*读取配置文件字符串数据*/
ssize_t size;
std::string pathKey = FileUtils::getInstance()->fullPathForFilename("Json/ship_config.json");
unsigned char*ch=FileUtils::getInstance()->getFileData(pathKey.c_str(),"r",&size);
std::string data=std::string((const char*)ch,size);
/*开始解析*/
rapidjson::Document doc;///<创建一个Document对象rapidJson的相关操作都在Document类中
doc.Parse<0>(data.c_str());///<通过Parse方法将Json数据解析出来
if(doc.HasParseError())
{
CCLOG("GetParseError%s\n",doc.GetParseError());
return false;
}
if(index>0)
{
int size = doc.Size();
for (int i = 0; i < size; ++i)
{
int id = doc[i]["id"].GetInt();
if (id == index)
{
char c[2];//!!!!!!!!!!!!!!!0~9个飞船,超过需扩展
sprintf(c,"%d",index-1);
const char*modePath =doc[i]["pic"].GetString();
CCLOG("ship path:%s",modePath);
std::string pre = "Image/SpaceShip/";
shipSp = Sprite::create(pre.append(modePath).append(".png"));
// shipSp->setAnchorPoint(Point(.5,.5));
Size ss= shipSp->getContentSize();
shipSp->setRotation(SHIPROTATION);
addChild(shipSp,SPACESHIP,SPACESHIP);
shieldValue = doc[i]["shield"].GetInt();
energy = doc[i]["energy"].GetInt();
shapeId = doc[i]["shape"].GetInt();
m_rate = doc[i]["rate"].GetDouble();
radius = ss.width*0.6;
break;
}
}
}
}while(0);
scheduleUpdate();
return true;
}
原文链接:https://www.f2er.com/cocos2dx/344653.html