6

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に翻訳する必要があります。私はいくつかの記事をグーグルで検索しましたが、それらはすべて 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>

私は何をすべきか???それでも同じエラーが発生します。

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

動作しません。まずはお試しください。サンプル plist から int を読み取れるかどうかを確認します

4

4 に答える 4

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

ルートがディクショナリの場合は、次のコードを試してください

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

クラスの中を見るCCParticleSystem.cppと、バッターのアイデアが浮かぶかもしれません。クラスbool CCParticleSystem::initWithDictionary(CCDictionary<std::string, CCObject*> *dictionary)内をチェックCCParticleSystem.cpp

よろしく、 ニキル

于 2012-05-17T10:49:11.343 に答える
3

here は、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に感謝します。

于 2012-05-17T10:56:59.893 に答える
2

dictを読み、を使用すると、次のようObjectForKey("BLA")に型キャストできます。CCString*

    CCString* tmpStr = (CCString*)(yourDict->ObjectForKey("KEY"));
    int x = tmpStr->toInt();
于 2012-05-29T10:37:28.460 に答える
0

あなたも使うことができます

Array* items = Array::createWithContentsOfFile("name.plist");
于 2013-11-13T01:19:00.110 に答える