#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;
}
总结以上是内存溢出为你收集整理的cocos2dx3.2 读取json全部内容,希望文章能够帮你解决cocos2dx3.2 读取json所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)