Cocos2D-X : 解析 TMX 文件中的对象列表,将代码从版本 2.X 迁移到版本 3.X

Cocos2D-X : Parsing list of objects from a TMX file, migrating code from version 2.X to version 3.X

我是 Cocos2D-X 的新手,过去几个月一直在努力学习它。我正在尝试根据 Cocos2d-x 游戏开发蓝图 一书创建一个示例游戏,在这一章中,它解释了如何使用平铺地图创建游戏。

我需要帮助解析 TMX 文件中的对象列表,我正在尝试将代码从版本 2.X 更新到版本 3.X,因为我遇到了编译错误。我需要在以下代码中将已弃用的 CCArray 和 CCDictionary 更改为新的 cocos::Vector 和 Map

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。有人可以帮我吗?

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();
}