2016-04-04 16 views
0

私はCocos2D-Xを初めて使用しています。 Cocos2d-x Game Development Blueprintsのサンプルゲームを作成しようとしています。この章では、タイルマップを使用してゲームを作成する方法について説明します。Cocos2D-X:バージョン2.Xからバージョン3.Xへのコードの移行、TMXファイルのオブジェクトのリストの解析

TMXファイルからオブジェクトのリストを解析するのに役立つ必要があります。コンパイルエラーが発生するため、バージョン2.Xからバージョン3.Xにコードを更新しようとしています。私は、次のコードを、私はそれを自分自身を変換しようとしている

void GameWorld::CreateTiledMap() 
{ 
// generate level filename 
char buf[128] = {0}; 
sprintf(buf, "level_%02d.tmx", GameGlobals::level_number_); 
// create & add the tiled map 
tiled_map_ = CCTMXTiledMap::create(buf); 
addChild(tiled_map_); 

// get the size of the tiled map 
columns_ = (int)tiled_map_->getMapSize().width; 
rows_ = (int)tiled_map_->getMapSize().height; 

// save a reference to the layer containing all the bricks 
bricks_layer_ = tiled_map_->layerNamed("Bricks"); 

// parse the list of objects 
CCTMXObjectGroup* object_group = tiled_map_->objectGroupNamed("Objects"); 
CCArray* objects = object_group->getObjects(); 
int num_objects = objects->count(); 

for(int i = 0; i < num_objects; ++i) 
{ 
    CCDictionary* object = (CCDictionary*)(objects->objectAtIndex(i)); 

    // create the Hero at this spawning point 
    if(strcmp(object->valueForKey("name")->getCString(), "HeroSpawnPoint") == 0) 
    { 
     CreateHero(ccp(object->valueForKey("x")->floatValue(), object->valueForKey("y")->floatValue())); 
    } 
    // create an Enemy at this spawning point 
    else if(strcmp(object->valueForKey("name")->getCString(), "EnemySpawnPoint") == 0) 
    { 
     CCPoint position = ccp(object->valueForKey("x")->floatValue(), object->valueForKey("y")->floatValue()); 
     CCPoint speed = ccp(object->valueForKey("speed_x")->floatValue(), object->valueForKey("speed_y")->floatValue()); 
     CreateEnemy(position, speed); 
    } 
    // create a Platform at this spawning point 
    else if(strcmp(object->valueForKey("name")->getCString(), "PlatformSpawnPoint") == 0) 
    { 
     CCPoint position = ccp(object->valueForKey("x")->floatValue(), object->valueForKey("y")->floatValue()); 
     CCPoint speed = ccp(object->valueForKey("speed_x")->floatValue(), object->valueForKey("speed_y")->floatValue()); 
     CreatePlatform(position, speed); 
    } 
    // save the point where the level should complete 
    else if(strcmp(object->valueForKey("name")->getCString(), "LevelCompletePoint") == 0) 
    { 
     level_complete_height_ = object->valueForKey("y")->floatValue(); 
    } 
} 
} 

に新しいココス::ベクトルと地図への非推奨CCArrayとCCDictionaryを変更する必要がありますが、成功していません。 CCArrayをcocos :: Vector、 に変換するのに問題がありますが、主にCCDictionaryをcocos :: Mapに変更しています。誰か助けてくれますか?

+0

エラーは何ですか?変更しようとしたことと失敗した理由を教えてください。 – rhughes

+0

@rhughes CCArray * objects = object_group-> getObjects();でエラーが発生しました。 "ValueVector '(別名'ベクトル ')から' CCArray * 'への実行可能な変換はありません。getObjects()はもはやCCArrayの一部ではなく、CCArray * objects = object_group- > getObjects();自動キーワードを使用するには、CCDictionary * object =(CCDictionary *)(object-> objectAtIndex(i))でエラーが発生します。 'value_type'(別名 'cocos2d :: Value')からポインタ型 'cocos2d :: __ Dictionary *'にキャストできません。 – VakHD

答えて

0

Cocos2d-xには多くのテストがあり、非常に役に立ちます。から\tests\cpp-tests\Classes\TileMapTest\TileMapTest.cpp

auto map = TMXTiledMap::create("TileMaps/test-object-layer.tmx"); 
addChild(map, -1, kTagTileMap); 

CCLOG("----> Iterating over all the group objets"); 

auto group = map->getObjectGroup("Objects"); 
auto& objects = group->getObjects(); 
for (auto& obj : objects) 
{ 
    ValueMap& dict = obj.asValueMap(); 

    float x = dict["x"].asFloat(); 
    string name = dict["name"].asString(); 
} 
+0

OPの質問に答えを適用できますか? – rhughes

+0

しかし、私は、このコードスニペットが、tmxファイルからオブジェクトグループとオブジェクトを取得し、それらのオブジェクトを解析する方法を明確に示していると思います。 @rhughes – Zen

関連する問題