Understanding How to Obtain All TiledMap Layer Names on cocos2d-x

Asked 2 years ago, Updated 2 years ago, 120 views

I wrote the following code using cocos2d-x ver3.6.

The purpose of this case is
The TMXTiledMap editor (non-programmers) can use the layer name in the TMXTiledMap. I would like to freely name it and allow it to be loaded on the program side.

automap=TMXTiledMap::create(FileName);

// acquire the topographic layer
autoterrainLayer=map->getLayer("Terrain");
if(nullptr==terrainLayer) {
    // TerrainLayer does not exist
}

For example, in the case of the above code, you can read it by using the layer name "Terrain" of the file you created.
I am having trouble using getLayer when the layer name is unknown.

I looked at the page below, but I was not sure, so I would like to ask you a question here.
http://www.cocos2d-x.org/reference/native-cpp/V3.0/d6/d48/classcocos2d_1_1_t_m_x_tiled_map.html

cocos2d-x

2022-09-30 19:28

2 Answers

The layer name exists to find the specified layer among multiple layers.
Of course, there must be an arrangement between the map creator and the programmer.
Otherwise, I have no idea which layer represents what.
There are ways to add properties, but you don't usually do it.


2022-09-30 19:28

This question is quite a long time ago, so it may have been solved, but

autotileMap=TMXTiledMap::create("filename");
    Vector<Node*>childOfMap=tileMap->getChildren();
    for (auto&child:childOfMap)
    {
        TMXLayer*layer=dynamic_cast<TMXLayer*>(child);
        if(layer!=nullptr){
            const std::string name = layer->getLayerName();
            CCLOG("%s", name.c_str());
        }
    }

So what do you think? Sorry if I misunderstood you


2022-09-30 19:28

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.