Cocos2d-x how to read plist in an array

I want to read 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 is basically a dictionary array consisting of (x, y) coordinates. my source code to read:

 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 was looking for an article, but they all relate to reading plist in a dictionary. I need an array.

EDIT:

Now I 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"); 

will not work. Try it first. see if you can read int from plist sample

+6
source share
4 answers

Try the following 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 it 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 the following code in case of root - Dictionary

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

Take a look inside the CCParticleSystem.cpp class you can get. check the bool CCParticleSystem::initWithDictionary(CCDictionary<std::string, CCObject*> *dictionary) inside the CCParticleSystem.cpp class

Regards, Nihil

+10
source

See here a link to read a dictionary from a file .
To read the array, I could not find anything, so you can 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 OMGPOP .

+3
source

when you read in a dict and use ObjectForKey("BLA") , you can give it the value CCString* as follows:

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

you can also use

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

Source: https://habr.com/ru/post/915980/


All Articles