# Cocos can't getLayer

**URL:** <https://discourse.mapeditor.org/t/cocos-cant-getlayer/2642>\
**Category:** Questions\
**Created:** [August 11, 2017, 2:09pm UTC](https://discourse.mapeditor.org/t/cocos-cant-getlayer/2642 "2017-08-11T14:09:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![armnotstrong](https://avatars.discourse-cdn.com/v4/letter/a/b5ac83/32.png) [@armnotstrong](https://discourse.mapeditor.org/u/armnotstrong)\
**Post date:** [August 11, 2017, 2:09pm UTC](https://discourse.mapeditor.org/t/cocos-cant-getlayer/2642/1 "2017-08-11T14:09:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bjorn](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/bjorn/32/1599_2.png) [@bjorn](https://discourse.mapeditor.org/u/bjorn)\
**Post date:** [August 11, 2017, 5:54pm UTC](https://discourse.mapeditor.org/t/cocos-cant-getlayer/2642/2 "2017-08-11T17:54:03Z")

</div>

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:

> **[cocos2d-x](http://discuss.cocos2d-x.org/c/cocos2d-x)**
>
> Discuss anything about cocos2d-x.

---

<div class="post-metadata">

**Author:** ![armnotstrong](https://avatars.discourse-cdn.com/v4/letter/a/b5ac83/32.png) [@armnotstrong](https://discourse.mapeditor.org/u/armnotstrong)\
**Post date:** [August 12, 2017, 2:59am UTC](https://discourse.mapeditor.org/t/cocos-cant-getlayer/2642/3 "2017-08-12T02:59:40Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![armnotstrong](https://avatars.discourse-cdn.com/v4/letter/a/b5ac83/32.png) [@armnotstrong](https://discourse.mapeditor.org/u/armnotstrong)\
**Post date:** [August 12, 2017, 4:29am UTC](https://discourse.mapeditor.org/t/cocos-cant-getlayer/2642/4 "2017-08-12T04:29:12Z")

</div>

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` 😃

---

<div class="post-metadata">

**Author:** ![bjorn](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/bjorn/32/1599_2.png) [@bjorn](https://discourse.mapeditor.org/u/bjorn)\
**Post date:** [August 12, 2017, 8:14am UTC](https://discourse.mapeditor.org/t/cocos-cant-getlayer/2642/5 "2017-08-12T08:14:45Z")

</div>

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.
