首页 > 代码库 > Cocos2d-x--使用CCDictionary读取plist文件

Cocos2d-x--使用CCDictionary读取plist文件

frames读取:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
      <dict>
      <key>frames</key>
        <dict>
            <key>game_start/bg_image.png</key>
            <dict>
                <key>width</key>
                <integer>640</integer>
                <key>height</key>
                <integer>1136</integer>
                <key>originalWidth</key>
                <integer>640</integer>
                <key>originalHeight</key>
                <integer>1136</integer>
                <key>x</key>
                <integer>0</integer>
                <key>y</key>
                <integer>0</integer>
                <key>offsetX</key>
                <real>0</real>
                <key>offsetY</key>
                <real>0</real>
            </dict>
            <key>game_start/logo.png</key>
            <dict>
                <key>width</key>
                <integer>509</integer>
                <key>height</key>
                <integer>416</integer>
                <key>originalWidth</key>
                <integer>509</integer>
                <key>originalHeight</key>
                <integer>416</integer>
                <key>x</key>
                <integer>0</integer>
                <key>y</key>
                <integer>1138</integer>
                <key>offsetX</key>
                <real>0</real>
                <key>offsetY</key>
                <real>0</real>
            </dict>
            <key>game_start/play.png</key>
            <dict>
                <key>width</key>
                <integer>361</integer>
                <key>height</key>
                <integer>142</integer>
                <key>originalWidth</key>
                <integer>361</integer>
                <key>originalHeight</key>
                <integer>142</integer>
                <key>x</key>
                <integer>511</integer>
                <key>y</key>
                <integer>1138</integer>
                <key>offsetX</key>
                <real>0</real>
                <key>offsetY</key>
                <real>0</real>
            </dict>
            <key>game_start/share.png</key>
            <dict>
                <key>width</key>
                <integer>168</integer>
                <key>height</key>
                <integer>113</integer>
                <key>originalWidth</key>
                <integer>168</integer>
                <key>originalHeight</key>
                <integer>113</integer>
                <key>x</key>
                <integer>0</integer>
                <key>y</key>
                <integer>1556</integer>
                <key>offsetX</key>
                <real>0</real>
                <key>offsetY</key>
                <real>0</real>
            </dict>
            <key>game_start/store.png</key>
            <dict>
                <key>width</key>
                <integer>168</integer>
                <key>height</key>
                <integer>113</integer>
                <key>originalWidth</key>
                <integer>168</integer>
                <key>originalHeight</key>
                <integer>113</integer>
                <key>x</key>
                <integer>170</integer>
                <key>y</key>
                <integer>1556</integer>
                <key>offsetX</key>
                <real>0</real>
                <key>offsetY</key>
                <real>0</real>
            </dict>
   </dict>


 

结构 :

level

Key

Value

1

frames

dict

2

game_start/bg_image.png

game_start/logo.png

game_start/play.png

game_start/share.png

game_start/store.png

dict

dict

dict

dict

dict

3

Width

Height

originalWidth

originalHeight

X

Y

offsetX

offsetY

integer

integer

integer

integer

integer

integer

real

real

 

重点 :key 永远都是一个字符串类型,value需要根据名称来判断是什么类型

 

1、设置文件路径

//相对路径
const char* plistpath="NewUi0.plist";
//通过CCFileUtils的fullPathFromFile方法获取绝对路径
CCFileUtils* fileutils=CCFileUtils::shareFileUtils();
std::string pfullpath=fileutils->fullPathForFileName(plistpath);
Const char* cfullpath = pfullpath.c_str();

2、通过上面获取到的路径加载plist文件到CCDictionary 获得plist的根节点 dict容器

CCDictionary* rootdic =CCDictionary::createWithContentsOfFile(cfullpath);

3、key frames是一个dict字典类型 我们先通过根节点获得frames的dict容器

//第一层frames 
CCDictionary* framesdic =dynamic_cast<CCDictionary*>rootdic->objectForKey("frames");

4、Frames 下有5个key 获取第二层下所有key值

CCArray* imgkeyArr =framedic->allkeys();     

5通过for循环遍历CCArray 获取所有key值 通过key值得到dict  

for(unsigned int i=0;i<imgkeyArr->count();i++)
    {
        //第二层key (level=2)
        CCString* ccstr1 =(CCString*)imgkeyArr->objectAtIndex(i);
        std::string imgKey = ccstr1->getCString();
        //输出第二层所有key值
        CCLOG("[%s]",imgKey.c_str());
        //第二层value dict 
        CCDictionary* imgdic =dynamic_cast<CCDictionary*>(frameDic->objectForKey(imgKey));
        CCArray* imginfoArr =imgdic->allKeys();
        for(unsigned int j=0;j<imginfoArr->count();j++)
        {
            //第三层所有key (level=3)
            CCString* ccstr2 =(CCString*)imginfoArr->objectAtIndex(j);
            std::string imginfoKey = ccstr2->getCString();
            //通过第三层key值找到value值
            const CCString* values = imgdic->valueForKey(imginfoKey);
            std::string str = values->getCString();
            CCLOG("%s:%s",imginfoKey.c_str(),str.c_str());
        }
    }

结果: