Cocos can't getLayer

Hi, I am using cocos2dx version 3.15.1 and want to import the tmx file to my game
Not sure if I can ask here, but when importing the generated tmx and get the layer, it just return null

the c++ code I use is as following:

auto tmap = TMXTiledMap::create("map/map.tmx");
auto layer = tmap->getLayer("gamemap"); //here layer is null
auto size = tmap->getMapSize();
CCLOG(" %f, %f", size.height, size.width);
auto sprite = layer->getTileAt(Vec2(1, 1));

And here is the tmx file opened as plain text:

<?xml version="1.0" encoding="UTF-8"?>
<map version="1.0" tiledversion="1.0.2" orientation="orthogonal" renderorder="right-down" width="7" height="7" tilewidth="100" tileheight="100" nextobjectid="1">
 <tileset firstgid="1" source="map.tsx"/>
 <layer name="gamemap" width="7" height="7">
  <data encoding="csv">
2,2,1,2,2,2,1,
2,2,2,2,2,1,1,
1,1,1,1,2,1,1,
1,1,2,1,1,2,1,
1,2,2,2,2,2,1,
1,1,1,1,1,1,1,
1,1,1,1,1,1,1
</data>
 </layer>
</map>

I have tried csv format as well as the base64 and xml format.

Not sure what I have done wrong, if some one could give a hint will be
appreciated.

Thanks

Your code looks fine as far as I can see, but I’ve never used cocos2dx. You may have more luck asking on the cocos2dx forums:

Thanks for reply,
I have already posted this problem on both the cocos2d-x forum and StackOverflow, but still get no luck either place, I am now presuming this maybe a bug of cocos2d-x? will try to build a minimum project from scratch to find it out, and when things are confirmed, will let people know here.

I have found this out that not only I should put the tmx file in the source folder but also I have to import tileset file tsx file in the same folder and as well as the image that I loaded as a tileset file too. but the error message that cocos2dx gives could be a misleading for the solution. My bad to not got tiled map fully understand. But it will be helpful to show the actual location of the material in the inspector that build the tmx :smiley:

1 Like

Thanks for letting us know what the problem was! Please provide feedback to the cocos2d-x community so they could maybe improve the error message.