Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cocos2d-x how to read plist into an array

i want to read a plist using cocos2d-x (c++) here is my 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>

it's basically an array of dictionary that consist of (x, y) coordinates. my original code for reading is:

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

but Now i need to translate it into cocos2d-x in c++. i've googled some article but they are all about reading plist into dictionary. i need an array.

EDIT:::

Now i've changed my plist format:

<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>

what should i do??? I still get the same error:

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

won't work. Please try it out first. see if you can read a int from the sample plist

like image 447
OMGPOP Avatar asked May 17 '12 10:05

OMGPOP


3 Answers

Try the follwing line of code

//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");

EDIT

or you can try this too...

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

EDIT-2

Try Following code in case of root is Dictionary

   var1 = atoi(valueForKey("blendFuncSource", dictionary));
    var2 = atoi(valueForKey("blendFuncDestination", dictionary));

Look Inside CCParticleSystem.cpp class you might get batter idea. check bool CCParticleSystem::initWithDictionary(CCDictionary<std::string, CCObject*> *dictionary) inside CCParticleSystem.cpp class

Regards, Nikhil

like image 97
NIKHIL Avatar answered Oct 08 '22 23:10

NIKHIL


See here is the link for reading a dictionary from file.
To read array I couldn't find any thing so what you can do is change your plist to

<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>

Then

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

Thanks to OMGPOP.

like image 30
Inder Kumar Rathore Avatar answered Oct 09 '22 00:10

Inder Kumar Rathore


when you read in a dict and using ObjectForKey("BLA"), you can typecast it to a CCString* like this :

    CCString* tmpStr = (CCString*)(yourDict->ObjectForKey("KEY"));
    int x = tmpStr->toInt();
like image 2
m.ding Avatar answered Oct 08 '22 23:10

m.ding