加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

cocos2dx3.2 读取json

发布时间:2020-12-14 20:13:30 所属栏目:百科 来源:网络整理
导读:#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

#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%sn",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;

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读