【问题标题】:Cocos2D-X : Parsing list of objects from a TMX file, migrating code from version 2.X to version 3.XCocos2D-X : 从 TMX 文件中解析对象列表,将代码从 2.X 版本迁移到 3.X 版本
【发布时间】:2016-04-04 00:21:27
【问题描述】:

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

【问题讨论】:

  • 有什么错误?您能否向我们展示您尝试更改的内容以及失败的原因?
  • @rhughes 我在 CCArray* objects = object_group->getObjects(); 收到错误它说“从 'ValueVector' (aka 'vector<:value>') 到 'CCArray 没有可行的转换”我认为 getObjects() 不再是 CCArray 的一部分,当我更改 CCArray 时 对象 = object_group->getObjects();要使用 auto 关键字,我在 CCDictionary* object = (CCDictionary*)(objects->objectAtIndex(i)) 处遇到错误;说:不能从类型'value_type'(又名'cocos2d::Value')转换为指针类型'cocos2d::__Dictionary *'

标签: c++ c++11 cocos2d-x


【解决方案1】:

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

【讨论】:

  • 请您将您的答案应用于OP的问题。
  • 但我认为这段代码 sn-p 已经清楚地展示了如何从 tmx 文件中获取对象组和对象,然后解析这些对象。 @rhughes
猜你喜欢
  • 1970-01-01
  • 2021-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多