先要做准备工作
先下载DS_Dictionary.h和Ds_Dictionary.cpp,不多说提供下载地址
http://www.cocos2d-x.org/boards/6/topics/6125?r=13203#message-13203
文件基于pugixml下面是下载地址
下面提供test.plist作对照
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
- <plist version="1.0">
- <dict>
- <key>texture</key>
- <dict>
- <key>width</key>
- <integer>512</integer>
- <key>height</key>
- <integer>1024</integer>
- </dict>
- <key>frames</key>
- <dict>
- <key>bg_0.png</key>
- <dict>
- <key>x</key>
- <integer>1</integer>
- <key>y</key>
- <integer>289</integer>
- <key>width</key>
- <integer>480</integer>
- <key>height</key>
- <integer>287</integer>
- <key>offsetX</key>
- <real>0</real>
- <key>offsetY</key>
- <real>1</real>
- <key>originalWidth</key>
- <integer>480</integer>
- <key>originalHeight</key>
- <integer>289</integer>
- </dict>
- <key>bg_2.png</key>
- <dict>
- <key>x</key>
- <integer>1</integer>
- <key>y</key>
- <integer>1</integer>
- <key>width</key>
- <integer>480</integer>
- <key>height</key>
- <integer>287</integer>
- <key>offsetX</key>
- <real>0</real>
- <key>offsetY</key>
- <real>0</real>
- <key>originalWidth</key>
- <integer>480</integer>
- <key>originalHeight</key>
- <integer>287</integer>
- </dict>
- <key>bg_1.png</key>
- <dict>
- <key>x</key>
- <integer>1</integer>
- <key>y</key>
- <integer>577</integer>
- <key>width</key>
- <integer>480</integer>
- <key>height</key>
- <integer>287</integer>
- <key>offsetX</key>
- <real>0</real>
- <key>offsetY</key>
- <real>0</real>
- <key>originalWidth</key>
- <integer>480</integer>
- <key>originalHeight</key>
- <integer>287</integer>
- </dict>
- </dict>
- </dict>
- </plist>
下面提供一个解析的函数
- void collision::plistParse()
- {
- //Load rootDict from file, and then step into the metadata sub dict.
- DS_Dictionary rootDict;
- if(!rootDict.loadRootSubDictFromFile(bgObjectPlist))
- {
- printf("no load\n");
- }
- if(!rootDict.stepIntoSubDictWithKey("texture"))
- {
- printf("No texture\n");
- }
- //Get an int value from the subdict
- int someInt = rootDict.getIntegerForKey("height");
- printf("height %d\n",someInt);
- someInt = rootDict.getIntegerForKey("width");
- printf("width %d\n",someInt);
- //Step out of the sub dict and into another
- rootDict.stepOutOfSubDict();
- if(!rootDict.stepIntoSubDictWithKey("frames"))
- {
- printf("no dic\n");
- }
- for(int i=0;i<3;)
- {
- char picture[20];
- sprintf(picture,"bg_%d.png",i);
- if(!rootDict.stepIntoSubDictWithKey(picture))
- {
- i++;
- //printf("No %s\n",picture);
- rootDict.stepOutOfSubDict();
- continue;
- }
- int x=rootDict.getIntegerForKey("x");
- int y=rootDict.getIntegerForKey("y");
- int width=rootDict.getIntegerForKey("width");
- int height=rootDict.getIntegerForKey("height");
- }
- void stepBackToRootSubDict();
- }