先看下我们需要读取的pilst文件 <plist version="1.0"> <dict> <key>Metadata</key> <dict> <key>format</key> <integer>1</integer> </dict> <key>bodies</key> <dict> <key>green</key> <dict> <key>anchorpoint</key> <string>{ 0.50000,0.50000 }</string> <key>fixtures</key> <array> <dict> <key>mass</key> <real>2</real> <key>elasticity</key> <real>0</real> <key>friction</key> <real>0.5</real> <key>surface_velocity</key> <string>{ 0.00000,0.00000 }</string> <key>layers</key> <integer>0</integer> <key>group</key> <integer>0</integer> <key>collision_type</key> <integer>0</integer> <key>isSensor</key> <false/> <key>fixture_type</key> <key>POLYGON</key> <key>polygons</key> <array> <array> <string>{ -50.00000,-38.00000 }</string> <string>{ -17.00000,48.00000 }</string> <string>{ 9.00000,0.00000 }</string> <string>{ 33.00000,-73.00000 }</string> </array> <array> <string>{ 23.00000,48.00000 }</string> <string>{ 90.00000,6.00000 }</string> <string>{ 9.00000,0.00000 }</string> <string>{ -17.00000,48.00000 }</string> </array> </array> </dict> </array> </dict> </dict> </dict> </plist>好了上代码
#pragma once #include"cocos2d.h" class ReadPlistFlie { public: ReadPlistFlie(); ~ReadPlistFlie(); /*获取dict文件*/ cocos2d::ValueMap getdictPlistbyName(std::string str); /*获取arr文件*/ cocos2d::ValueVector getarrayPlistbyName(std::string str); /*获取dict中的dict*/ cocos2d::ValueMap getPilstDict(cocos2d::ValueMap vmap,std::string key); /*获取arr下标字典*/ cocos2d::ValueMap getPilstDict(cocos2d::ValueVector vvector,int idex); /*获取dict中的cocos2d::String* */ std::string getPlistString(cocos2d::ValueMap vmap,std::string key); /*获取dict中的float*/ float getPlistfloat(cocos2d::ValueMap vmap,std::string key); /*获取dict中的int*/ int getPlistInt(cocos2d::ValueMap vmap,std::string key); /*获取dict中的bool*/ static bool getPlistBool(cocos2d::ValueMap vmap,std::string key); /*获取dict中的array*/ cocos2d::ValueVector getPlistArray(cocos2d::ValueMap vmap,std::string key); /*获取arrayx下标中的array*/ cocos2d::ValueVector getPlistArray(cocos2d::ValueVector vvector,int idex); /*获取dict中的Point*/ cocos2d::Vec2 getPilstPoint(cocos2d::ValueMap vmap,std::string key); /*获取array下标中的point*/ cocos2d::Vec2 getPilstPoint(cocos2d::ValueVector vvector,int idex); }; #include "ReadPlistFlie.h" USING_NS_CC; ReadPlistFlie::ReadPlistFlie() { } ReadPlistFlie::~ReadPlistFlie() { } cocos2d::ValueMap ReadPlistFlie::getdictPlistbyName(std::string str) { ValueMap valuemap = FileUtils::getInstance()->getValueMapFromFile(str.c_str()); return valuemap; } cocos2d::ValueVector ReadPlistFlie::getarrayPlistbyName(std::string str) { ValueVector valuevector = FileUtils::getInstance()->getValueVectorFromFile(str.c_str()); return valuevector; } cocos2d::ValueMap ReadPlistFlie::getPilstDict(cocos2d::ValueMap vmap,std::string key) { ValueMap valuemap = vmap.at(key.c_str()).asValueMap(); return valuemap; } cocos2d::ValueMap ReadPlistFlie::getPilstDict(cocos2d::ValueVector vvector,int idex) { ValueMap valuemap = vvector.at(idex).asValueMap(); return valuemap; } std::string ReadPlistFlie::getPlistString(cocos2d::ValueMap vmap,std::string key) { std::string str = vmap.at(key.c_str()).asString(); return str; } float ReadPlistFlie::getPlistfloat(cocos2d::ValueMap vmap,std::string key) { float valuefloat = vmap.at(key.c_str()).asFloat(); return valuefloat; } int ReadPlistFlie::getPlistInt(cocos2d::ValueMap vmap,std::string key) { int valueint = vmap.at(key.c_str()).asInt(); return valueint; } bool ReadPlistFlie::getPlistBool(cocos2d::ValueMap vmap,std::string key) { bool valuebool = vmap.at(key.c_str()).asBool(); return valuebool; } cocos2d::ValueVector ReadPlistFlie::getPlistArray(cocos2d::ValueMap vmap,std::string key) { ValueVector valuevector = vmap.at(key.c_str()).asValueVector(); return valuevector; } cocos2d::ValueVector ReadPlistFlie::getPlistArray(cocos2d::ValueVector vvector,int idex) { ValueVector valuevector = vvector.at(idex).asValueVector(); return valuevector; } cocos2d::Vec2 ReadPlistFlie::getPilstPoint(cocos2d::ValueMap vmap,std::string key) { Vec2 points = cocos2d::PointFromString(vmap.at(key.c_str()).asString()); return points; } cocos2d::Vec2 ReadPlistFlie::getPilstPoint(cocos2d::ValueVector vvector,int idex) { Vec2 points = cocos2d::PointFromString(vvector.at(idex).asString()); return points; }
写个使用例子
ValueMap plistDic = getdictPlistbyName("bodySprite.plist"); ValueMap Metadata = getPilstDict(plistDic,"Metadata"); int format = getPlistInt(Metadata,"format"); log("asdad %d",format); ValueMap bodies = getPilstDict(plistDic,"bodies"); ValueMap green = getPilstDict(bodies,"green"); ValueVector fixtures = getPlistArray(green,"fixtures"); ValueMap dicc = getPilstDict(fixtures,0); ValueVector polygons = getPlistArray(dicc,"polygons"); for(int i = 0; i< polygons.size();i++) { ValueVector pointarray = getPlistArray(polygons,i); for (int j = 0; j < pointarray.size();j++) { Vec2 points = getPilstPoint(pointarray,j); log("x %f y %f",points.x,points.y); } }
好了看下运行结果
asdad 1 x -50.000000 y -38.000000 x -17.000000 y 48.000000 x 9.000000 y 0.000000 x 33.000000 y -73.000000 x 23.000000 y 48.000000 x 90.000000 y 6.000000 x 9.000000 y 0.000000 x -17.000000 y 48.000000
原文链接:https://www.f2er.com/cocos2dx/345966.html