cocos2d-x如何将plist读入数组

时间:2012-05-17 10:32:13

标签: iphone ios cocos2d-iphone plist cocos2d-x

我想用cocos2d-x(c ++)读取一个plist 这是我的plist:

<array>
    <dict>
        <key>x</key>
        <integer>0</integer>
        <key>y</key>
        <integer>0</integer>
    </dict>
    <dict>
        <key>x</key>
        <integer>140</integer>
        <key>y</key>
        <integer>12</integer>
    </dict>
    <dict>
        <key>x</key>
        <integer>120</integer>
        <key>y</key>
        <integer>280</integer>
    </dict>
    <dict>
        <key>x</key>
        <integer>40</integer>
        <key>y</key>
        <integer>364</integer>
    </dict>
<array>

它基本上是由(x,y)坐标组成的字典数组。 我原来的阅读代码是:

NSString *path = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"w%i", world] ofType:@"plist"];
NSMutableArray* points = [NSMutableArray arrayWithContentsOfFile:path];

但现在我需要将其翻译成c ++中的cocos2d-x。我用Google搜索了一些文章,但它们都是关于将plist读入字典。我需要一个阵列。

EDIT :::

现在我改变了我的plist格式:

<dict>
    <key>11x</key>
    <integer>0</integer>
    <key>11y</key>
    <integer>0</integer>
    <key>12x</key>
    <integer>140</integer>
    <key>12y</key>
    <integer>12</integer>
<dict>

我该怎么办?我仍然得到同样的错误:

CCDictionary<std::string, CCObject*>* dict = CCFileUtils::dictionaryWithContentsOfFile(plistPath);
int x = (int)dict->objectForKey("11x");
int y = (int)dict->objectForKey("11y");

不起作用。 请先试一试。看看你是否可以从样本plist中读取一个int

4 个答案:

答案 0 :(得分:10)

尝试以下代码

//const char *pszPath = CCFileUtils::fullPathFromRelativePath(plistName);
//consider that file is in resource bundle..
// CCDictionary<std::string, CCObject*> *plistDictionary=CCFileUtils::dictionaryWithContentsOfFile("testplist.plist");
// CCArray *testitems = (CCArray*)plistDictionary->objectForKey("root");

编辑

或者你也可以尝试一下......

 CCDictionary<std::string, CCObject*> *plistDictionary = CCFileUtils::dictionaryWithContentsOfFile("testplist.plist");
 CCMutableArray<CCObject*> *testitems = (CCMutableArray<CCObject*>*)plistDictionary->objectForKey("root");
 CCLog("COUNT: %d", testitems->count());

修改-2

如果root是Dictionary

,请尝试以下代码
   var1 = atoi(valueForKey("blendFuncSource", dictionary));
    var2 = atoi(valueForKey("blendFuncDestination", dictionary));

查看内部CCParticleSystem.cpp课程,您可能会有更好的想法。 检查bool CCParticleSystem::initWithDictionary(CCDictionary<std::string, CCObject*> *dictionary)

中的CCParticleSystem.cpp

此致 NIKHIL

答案 1 :(得分:3)

这里是link for reading a dictionary from file
要读取数组,我找不到任何东西,所以你可以做的是将你的plist改为

<dict> <key>root</key>
  <array>
    <dict>
        <key>x</key>
        <integer>0</integer>
        <key>y</key>
        <integer>0</integer>
    </dict>
    <dict>
        <key>x</key>
        <integer>140</integer>
        <key>y</key>
        <integer>12</integer>
    </dict>
    <dict>
        <key>x</key>
        <integer>120</integer>
        <key>y</key>
        <integer>280</integer>
    </dict>
    <dict>
        <key>x</key>
        <integer>40</integer>
        <key>y</key>
        <integer>364</integer>
    </dict>
  <array>
</dict>

然后

CCDictionary<std::string, CCObject*> *dict = CCFileUtils::dictionaryWithContentsOfFile("yourFile.plist");
CCArray *testitems = (CCArray*)dict->objectForKey("root");

感谢OMGPOP

答案 2 :(得分:2)

当您在dict中阅读并使用ObjectForKey("BLA")时,您可以将其类型转换为CCString*,如下所示:

    CCString* tmpStr = (CCString*)(yourDict->ObjectForKey("KEY"));
    int x = tmpStr->toInt();

答案 3 :(得分:0)

您也可以使用

Array* items = Array::createWithContentsOfFile("name.plist");