cocos2dx3.2 读取json

前端之家收集整理的这篇文章主要介绍了cocos2dx3.2 读取json前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

#include "json/rapidjson.h"@H_403_5@

#include "json/Document.h"@H_403_5@

#include "json/reader.h"@H_403_5@

#include "json/writer.h"@H_403_5@

#include "cocos2d.h"@H_403_5@

USING_NS_CC;@H_403_5@


@H_403_5@

bool SpaceShip::init(ValueMap&vm,int index){@H_403_5@


@H_403_5@

@H_403_5@

do{@H_403_5@

/*读取配置文件字符串数据*/@H_403_5@

@H_403_5@

ssize_t size;@H_403_5@

std::string pathKey = FileUtils::getInstance()->fullPathForFilename("Json/ship_config.json");@H_403_5@

unsigned char*ch=FileUtils::getInstance()->getFileData(pathKey.c_str(),"r",&size);@H_403_5@

std::string data=std::string((const char*)ch,size);@H_403_5@

@H_403_5@

/*开始解析*/@H_403_5@

rapidjson::Document doc;///<创建一个Document对象rapidJson的相关操作都在Document类中@H_403_5@

doc.Parse<0>(data.c_str());///<通过Parse方法Json数据解析出来@H_403_5@

if(doc.HasParseError())@H_403_5@

{@H_403_5@

CCLOG("GetParseError%s\n",doc.GetParseError());@H_403_5@

return false;@H_403_5@

@H_403_5@

}@H_403_5@

@H_403_5@

@H_403_5@

if(index>0)@H_403_5@

{@H_403_5@

int size = doc.Size();@H_403_5@

for (int i = 0; i < size; ++i)@H_403_5@

{@H_403_5@

int id = doc[i]["id"].GetInt();@H_403_5@

@H_403_5@

if (id == index)@H_403_5@

{@H_403_5@


@H_403_5@

char c[2];//!!!!!!!!!!!!!!!0~9个飞船,超过需扩展@H_403_5@

sprintf(c,"%d",index-1);@H_403_5@

const char*modePath =doc[i]["pic"].GetString();@H_403_5@

CCLOG("ship path:%s",modePath);@H_403_5@

std::string pre = "Image/SpaceShip/";@H_403_5@

shipSp = Sprite::create(pre.append(modePath).append(".png"));@H_403_5@

// shipSp->setAnchorPoint(Point(.5,.5));@H_403_5@

Size ss= shipSp->getContentSize();@H_403_5@

@H_403_5@

shipSp->setRotation(SHIPROTATION);@H_403_5@

addChild(shipSp,SPACESHIP,SPACESHIP);@H_403_5@

shieldValue = doc[i]["shield"].GetInt();@H_403_5@

energy = doc[i]["energy"].GetInt();@H_403_5@

shapeId = doc[i]["shape"].GetInt();@H_403_5@

m_rate = doc[i]["rate"].GetDouble();@H_403_5@

radius = ss.width*0.6;@H_403_5@

break;@H_403_5@

}@H_403_5@

}@H_403_5@

}@H_403_5@

}while(0);@H_403_5@

scheduleUpdate();@H_403_5@

return true;@H_403_5@

}@H_403_5@ 原文链接:https://www.f2er.com/cocos2dx/344653.html

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