I am attempting to load a 2d tiled map (.tmx + .tms files) generated from Tiled Map Editor into Cocos2d-x version 4. I am building the project using Visual Studios 2022 -- Win32
I'm trying to make the scene as simple as possible, and the tiled map to be as simple as possible to just get it to work, -- to no avail.
Below is the full code of the Scene I'm trying to load:
#include "cocos2d.h"
USING_NS_CC;
class TileTestScene : public Scene
{
public:
virtual bool init()
{
auto map = TMXTiledMap::create("tmx/Trees.tmx");
layer = map->getLayer("Trees");
auto mapSize = layer->getLayerSize();
auto visibleSize = Director::getInstance()->getVisibleSize();
for (int i = 0; i < mapSize.width; ++i)
{
for (int j = 0; j < mapSize.height; ++j)
{
auto sprite = layer->getTileAt(Vec2(i, j));
if (!sprite)
{
}
}
}
this->addChild(map);
map->setPosition(visibleSize.width / 2.0f, visibleSize.height / 2.0f);
return true;
}
CREATE_FUNC(TileTestScene)
private:
TMXLayer* layer;
};
Below is the Trees.tmx file generated by Tiled Editor:
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.10" tiledversion="1.10.1" orientation="orthogonal" renderorder="right-up" width="4" height="4" tilewidth="34" tileheight="34" infinite="0" nextlayerid="2" nextobjectid="1">
<tileset firstgid="1" source="trees.tsx"/>
<layer id="1" name="Trees" width="4" height="4">
<data encoding="csv">
1,7,4,2,
3,8,5,6,
1,7,4,2,
3,8,5,6
</data>
</layer>
</map>
And below is trees.tsx
<?xml version="1.0" encoding="UTF-8"?>
<tileset version="1.10" tiledversion="1.10.1" name="trees" tilewidth="34" tileheight="34" tilecount="9" columns="3">
<image source="trees.png" width="102" height="102"/>
</tileset>
And image to the right is Trees.png, referenced by the tsx file:
In the tiled map editor, my map should look like the following (when zoomed in):
But, when then running Cocos2d-x-4 I get the following completely wrong output:
One thing of note which I found during debugging, which I cannot understand is the on the code line of:
auto sprite = layer->getTileAt(Vec2(i, j));
it sometimes returns back NULL sprites, even though there are no blank tiles on my map. I'm unsure if I have set something up incorrectly for Cocos2d-x or something else. Please help
UPDATE:
Cocos2d-x fails load the file properly because Tiled Editor version 1.10 added in newline characters in the csv data, -- breaking the Cocos2d-x importing the data.
Changing Tiled Editor to export data in base64 works. -- However, I would still like the csv format to work too.