2016-04-04 94 views
0

我是Cocos2D-X的新手,並且在過去的幾個月裏一直在嘗試學習它。我正在嘗試從書籍Cocos2d-x遊戲開發藍圖中創建一個示例遊戲,並且在這個特殊的章節中,它解釋瞭如何使用平鋪地圖創建遊戲。Cocos2D-X:解析TMX文件中的對象列表,將代碼從版本2.X遷移到版本3.X

I需要幫助解析TMX文件中的對象列表,我試圖將代碼從版本2.X更新到版本3.X,因爲我得到編譯錯誤。我需要改變過時CCArray和CCDictionary到新茯苓:: Vector和地圖在下面的代碼

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轉換爲cocos :: Vector, ,但主要是將CCDictionary更改爲cocos :: Map。有人可以幫幫我嗎?

+0

什麼是錯誤?你能告訴我們你試圖改變什麼,爲什麼它失敗了嗎? – rhughes

+0

@rhughes我在CCArray * objects = object_group-> getObjects()時出錯。並且它說「沒有從'ValueVector'(又名'vector ')到'CCArray *'的可行轉換我認爲getObjects()不再是CCArray的一部分,並且當我更改CCArray * objects = object_group- > getObjects();使用auto關鍵字我得到一個錯誤在CCDictionary * object =(CCDictionary *)(objects-> objectAtIndex(i));說:不能從類型'value_type'(aka'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