2012-05-17 8 views
6

へのplistを読むために ここでは私のplistです:cocos2d-xの私はcocos2d-X(C++)を使用した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]; 

しかし、今私はcocos2d-xのC++に翻訳する必要があります。私はいくつかの記事をgoogledているが、彼らはすべて辞書に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> 

私は何をすべき?私はまだ同じエラーが発生します:

は動作しません。 まず試してみてください。あなたがサンプルPLISTからint型を読み取ることができる場合

+0

を使用することができますcoco2d-Xサポートplistを読む。しかし、それはxmlを読むことをサポートする。だからxmlを解析して値を取得することができます –

+0

?私はそれがplistをサポートし、辞書に変換すると思います。 (FileUtil何かを使用してください) – OMGPOP

+0

はxml形式でplist自体ではありませんか? – OMGPOP

答えて

10

は、コードのfollwing行を試してみてください参照

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

か、これも...

CCDictionary<std::string, CCObject*> *plistDictionary = CCFileUtils::dictionaryWithContentsOfFile("testplist.plist"); 
CCMutableArray<CCObject*> *testitems = (CCMutableArray<CCObject*>*)plistDictionary->objectForKey("root"); 
CCLog("COUNT: %d", testitems->count()); 
を試すことができます

EDIT-2

ルートの場合は次のコード

てみますが、打者のアイデアを得る可能性があります辞書

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

ルックインサイドCCParticleSystem.cppクラスです。 bool CCParticleSystem::initWithDictionary(CCDictionary<std::string, CCObject*> *dictionary)CCParticleSystem.cpp内部クラス

よろしく、 ニキル

+0

どのように私は各値(int)を得ることができますか? – OMGPOP

+0

CCD辞書 point = testitems-> objectAtIndex(index); しかし、エラー – OMGPOP

+0

配列の各エントリは辞書です – OMGPOP

3

はここを参照してください チェックがlink for reading a dictionary from fileです。配列を読み取ること
私は任意のものがそうあなたが何ができるかを次に

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

OMGPOP

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

感謝をあなたのplistを変更で見つけることができませんでした。

+0

しかし、どのように配列から値(int)を取得できますか?私はobjectatindexを使用すると、インデックス内に入るようにインデックスを設定しても、インデックスの外にあるccobject – OMGPOP

+0

hiを返します。 – OMGPOP

+0

"ルート"が存在しないようです。あなたが私の答えにこのキーを追加する必要がありますので、 – OMGPOP

2

あなたが辞書で読み、ObjectForKey("BLA")を使用して、あなたはこのようなCCString*にそれを型キャストすることができます

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

あなたはまた、私は考えていない

Array* items = Array::createWithContentsOfFile("name.plist"); 
関連する問題