一码胜万言(请看注释)
CclUtil.h
// // CclUtil.h // PracticeDemo // // Created by kodeyang on 8/1/13. // // #ifndef __Practice_Demo__CclUtil__ #define __Practice_Demo__CclUtil__ #include "cocos2d.h" #include <string> using namespace cocos2d; using namespace std; /** * cocos2d-x 读取 Json 文件并做解析 * 依赖 jsoncpp 库。 * 注意,cocos2d-x 的 extension 中包含了另一个 json.h 头文件, * 而 jsoncpp 所要引入的头文件也是 json.h,这就导致如果不做改名处理的话编译将很难通过, * 我也是在将 jsoncpp 库的头文件 json.h 改成 jsoncpp.h 之后才解决编译问题的。 * 最后,不要忘记在 Header Search Paths 中将 jsoncpp 库所处的目录加上。 */ void parse(CCLayer* in_pLyr, const char* in_pArrCharCclName); #endif /* defined(__Practice_Demo__CclUtil__) */
CclUtil.cpp
// // CclUtil.cpp // PracticeDemo // // Created by kodeyang on 8/1/13. // // #include "CclUtil.h" #include "Jsoncpp.h" void parse(CCLayer* in_pLyr, const char* in_pArrCharCclName) { CCFileUtils* t_pFileUtils = CCFileUtils::sharedFileUtils(); string t_oStrFullPath = t_pFileUtils->fullPathForFilename(in_pArrCharCclName); unsigned char* t_pArrCharBuffer = NULL; unsigned long t_lBufferSize = 0; t_pArrCharBuffer = t_pFileUtils->getFileData(t_oStrFullPath.c_str(), "r", &t_lBufferSize); printf("%ld", t_lBufferSize); string t_oStrContent((char*)t_pArrCharBuffer, t_lBufferSize); printf("读取文件的内容为:%s", t_oStrContent.c_str()); Json::Value t_oValueSprites; Json::Reader t_oReader; t_oReader.parse(t_oStrContent, t_oValueSprites); for (int i = 0; i < t_oValueSprites.size(); i ++) { Json::Value tmp_oValueSprite = t_oValueSprites[i]; string tmp_oStrName = tmp_oValueSprite["image"].asString(); printf("精灵所使用的图片名称为 %s\n", tmp_oStrName.c_str()); CCSprite* sprite = CCSprite::create(tmp_oStrName.c_str()); float x = tmp_oValueSprite["x"].asDouble(); float y = tmp_oValueSprite["y"].asDouble(); sprite->setPosition(ccp(x, y)); in_pLyr->addChild(sprite); } if (t_pArrCharBuffer) { delete [] t_pArrCharBuffer; t_pArrCharBuffer = NULL; } }
QuickLayer.h
// // QuickLayer.h // TestSocket // // Created by kodeyang on 7/18/13. // // #ifndef TestSocket_QuickLayer_h #define TestSocket_QuickLayer_h #include "cocos2d.h" using namespace cocos2d; class QuickLayer : public CCLayer { public: static CCScene* scene(); CREATE_FUNC(QuickLayer); virtual bool init(); private: }; #endif
QuickLayer.cpp
// // QuickLayer.cpp // TestSocket // // Created by kodeyang on 7/18/13. // // #include "QuickLayer.h" #include "CclUtil.h" CCScene* QuickLayer::scene() { // 'scene' is an autorelease object CCScene* scene = CCScene::create(); // 'layer' is an autorelease object QuickLayer* layer = QuickLayer::create(); // add layer as a child to scene scene->addChild(layer); // return the scene return scene; } // on "init" you need to initialize your instance bool QuickLayer::init() { if (!CCLayer::init()) { return false; } parse(this, "project.ccl"); return true; }
project.ccl
[ { "available": true, "clickable": false, "desc": "描述...", "height": 480, "image": "bg.png", "visible": true, "width": 320, "x": 160, "y": 240, "zorder": -999 }, { "available": true, "clickable": true, "desc": "描述...", "height": 30, "image": "btn_ok.png", "visible": true, "width": 50, "x": 155, "y": 414, "zorder": 1 }, { "available": true, "clickable": true, "desc": "描述...", "height": 40, "image": "btn_options.png", "visible": true, "width": 80, "x": 151, "y": 140, "zorder": 2 } ]