cocos2d-x如何将plist读入数组

OMG*_*POP 6 iphone plist cocos2d-iphone ios 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>
Run Code Online (Sandbox Code Playgroud)

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

NSString *path = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"w%i", world] ofType:@"plist"];
NSMutableArray* points = [NSMutableArray arrayWithContentsOfFile:path];
Run Code Online (Sandbox Code Playgroud)

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

编辑:::

现在我改变了我的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>
Run Code Online (Sandbox Code Playgroud)

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

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

不行.请先试一试.看看你是否可以从样本plist中读取一个int

NIK*_*HIL 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");
Run Code Online (Sandbox Code Playgroud)

编辑

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

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

编辑-2

如果root是Dictionary,请尝试以下代码

   var1 = atoi(valueForKey("blendFuncSource", dictionary));
    var2 = atoi(valueForKey("blendFuncDestination", dictionary));
Run Code Online (Sandbox Code Playgroud)

CCParticleSystem.cpp课堂上看你可能会有更好的主意.检查bool CCParticleSystem::initWithDictionary(CCDictionary<std::string, CCObject*> *dictionary)内部CCParticleSystem.cpp课程

此致,Nikhil