Cocos2d-x TiledMap,在调用getLayer()获取碰撞层时返回nullptr?

时间:2018-05-15 17:19:43

标签: c++ cocos2d-x null-pointer

我一直在努力使用null指针几个小时!当我使用getLayer()方法从cocos2d-x中的平铺地图中获取图层时(使用尾部地图编辑器编辑了尾部地图),就发生了这种情况。

map = TMXTiledMap::create("map/map1.tmx");
map->setAnchorPoint(Vec2(0.5, 0.5));
map->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 2 - 40));
this->addChild(map, 0, 100);
collidable = map->getLayer("collide")
collidable->setVisible(false);

程序没有在我的尾部地图中找到“碰撞”图层(它是一个平铺图层)。但是,它确实存在于TMX文件中:

<?xml version="1.0" encoding="UTF-8"?>
<map version="1.0" tiledversion="1.1.5" orientation="orthogonal" renderorder="right-down" width="24" height="18" tilewidth="40" tileheight="40" infinite="0" nextlayerid="14" nextobjectid="2">

 <tileset firstgid="1" name="Background" tilewidth="40" tileheight="40" tilecount="7" columns="7">
  <image source="mapItems/background.png" width="280" height="40"/>
 </tileset>
 <tileset firstgid="8" name="tiles" tilewidth="40" tileheight="40" tilecount="16" columns="8">
  <image source="mapItems/tiles.png" width="320" height="80"/>
 </tileset>
 <tileset firstgid="24" source="mapItems/collide.tsx"/>
 <layer id="5" name="background" width="24" height="18">
  <data encoding="base64" compression="zlib">
   ...(some code)
 </layer>
 <layer id="11" name="collide" width="24" height="18">
  <properties>
   <property name="collidable" value="true"/>
  </properties>
  <data encoding="base64" compression="zlib">
   ...
  </data>
 </layer>
 <layer id="6" name="bricks" width="24" height="18">
  <data encoding="base64" compression="zlib">
   ...
  </data>
 </layer>
 <layer id="7" name="tops" width="24" height="18">
  <data encoding="base64" compression="zlib">
   ...
  </data>
 </layer>
</map>

此外,我在getLayer()方法中设置断点,程序遍历并查找除碰撞层之外的所有层。任何人都可以告诉我如何处理这个问题?

1 个答案:

答案 0 :(得分:0)

好的,最后我解决了这个问题(发生了4个小时)......尽管我甚至不知道为什么。 解决方案是编辑collide.tsx文件。我试着读了它,发现了一句话:

 <image source="collide.png" trans=ff00ff width="40" height="40"/>

这个&#34; trans =&#34;看起来很奇怪...所以我删除了它。然后问题就解决了。