0
\$\begingroup\$

I'm running into an issue with a Tiled map used as a background image for a scrolling scene. I'm using two sprite sheets to provide assets for a background layer. In Tiled, all of these assets appear correctly, but in cocos2d-x, the assets from WorldSelectBG01.png do not appear. Any thoughts on what might be going on here?

Here's what the TMX file looks like:

<?xml version="1.0" encoding="UTF-8"?>
<map version="1.0" tiledversion="1.0.3" orientation="orthogonal" renderorder="right-down" width="1" height="10" tilewidth="640" tileheight="640" nextobjectid="15">
 <tileset firstgid="1" name="WorldSelectBG01" tilewidth="640" tileheight="640" tilecount="6" columns="3">
  <image source="WorldSelectBG01.png" width="1920" height="1280"/>
 </tileset>
 <tileset firstgid="7" name="WorldSelectBG02" tilewidth="640" tileheight="640" tilecount="6" columns="3">
  <image source="WorldSelectBG02.png" width="1920" height="1280"/>
 </tileset>
 <layer name="Background" width="1" height="10">
  <data encoding="csv">
1,
2,
3,
4,
5,
7,
8,
9,
10,
11
</data>
 </layer>
 <objectgroup name="GameSelectButtons">
  <object id="1" name="gd0" x="394" y="6308"/>
  <object id="5" name="gd1" x="251" y="6253"/>
  <object id="6" name="gd2" x="140" y="6113"/>
  <object id="7" name="gd3" x="227" y="5972"/>
  <object id="8" name="gd4" x="362" y="5907"/>
  <object id="9" name="gd5" x="499" y="5806"/>
  <object id="10" name="gd6" x="458" y="5651"/>
  <object id="11" name="gd7" x="336" y="5583"/>
  <object id="12" name="gd8" x="241" y="5477"/>
  <object id="13" name="gd9" x="294" y="5365"/>
  <object id="14" name="gd10" x="322" y="5213"/>
 </objectgroup>
</map>

And in cocos2d-x, I'm reading it like this:

void ABTScene::AddBackgroundTileMap(const std::string& backgroundTiledMapFilename)
{
    if (backgroundTiledMapFilename.empty())
    {
        return;
    }

    const cocos2d::Size visibleSize{ cocos2d::Director::getInstance()->getVisibleSize() };
    const cocos2d::Vec2 origin{ cocos2d::Director::getInstance()->getVisibleOrigin() };

    _backgroundTiledMap = cocos2d::TMXTiledMap::create(backgroundTiledMapFilename);
    CC_ASSERT(_backgroundTiledMap);
    if (_backgroundTiledMap)
    {
        CCLOG("Background tile map read from resource file: %s", _backgroundTiledMap->getResourceFile().c_str());
        cocos2d::Vector<cocos2d::TMXObjectGroup*>& objectGroups{ _backgroundTiledMap->getObjectGroups() };
        for (const cocos2d::TMXObjectGroup* objectGroup : objectGroups)
        {
            CCLOG("Object group found in tilemap: %s", objectGroup->getGroupName().c_str());
        }

        // all tiles are aliased by default, let's set them anti-aliased
        for (const auto& child : _backgroundTiledMap->getChildren())
        {
            static_cast<cocos2d::SpriteBatchNode*>(child)->getTexture()->setAntiAliasTexParameters();
        }

        _backgroundTiledMap->setAnchorPoint(cocos2d::Vec2(0.0f, 0.0f));
        _backgroundTiledMap->setPosition(origin);
        const float bgImageScaleFactor = visibleSize.width / _backgroundTiledMap->getContentSize().width;
        _backgroundTiledMap->setScale(bgImageScaleFactor);

        this->addChild(_backgroundTiledMap, -1);
    }
}
\$\endgroup\$
  • 1
    \$\begingroup\$ I believe the problem could be that cocos2d-x still only supports one tileset per layer. \$\endgroup\$ – Thorbjørn Lindeijer Dec 30 '17 at 16:58
  • \$\begingroup\$ Thanks for this! I've separated the background layer into 4 individual layers and split the tiles into 4 tilesets. Each layer draws tiles from only one tileset, but so far this approach hasn't corrected the problem - is there something else I need to do? Thanks again! \$\endgroup\$ – Kevin Mack Jan 18 '18 at 16:48
  • 1
    \$\begingroup\$ I'm sorry to hear that it didn't help. I don't have any further suggestions, other than to ask in the cocos2d-x community. \$\endgroup\$ – Thorbjørn Lindeijer Jan 19 '18 at 19:29
  • 1
    \$\begingroup\$ Actually it turns out that the second error was my own - I had forgotten to add the additional tilesets to my resources and load them into the sprite cache. Once I took care of this, it worked. Thank you so much! \$\endgroup\$ – Kevin Mack Jan 22 '18 at 16:51
  • 1
    \$\begingroup\$ That's great and you're welcome! \$\endgroup\$ – Thorbjørn Lindeijer Jan 23 '18 at 19:45
1
\$\begingroup\$

Thanks to Thorbjørn Lindeijer for the answer!

The problem, as he advised, was that cocos2d-x only allows one tileset per layer - separating the background into layers solved the problem.

Updated file looks like this, and works. (Compression obscures the details, but each tile layer is only using assets from its paired tileset.):

<?xml version="1.0" encoding="UTF-8"?>
<map version="1.0" tiledversion="1.0.3" orientation="orthogonal" renderorder="right-up" width="1" height="18" tilewidth="640" tileheight="640" nextobjectid="15">
 <tileset firstgid="1" name="WorldSelectBG01" tilewidth="640" tileheight="640" tilecount="6" columns="3">
  <image source="WorldSelectBG01.png" width="1920" height="1280"/>
 </tileset>
 <tileset firstgid="7" name="WorldSelectBG02" tilewidth="640" tileheight="640" tilecount="4" columns="2">
  <image source="WorldSelectBG02.png" width="1280" height="1280"/>
 </tileset>
 <tileset firstgid="11" name="WorldSelectBG03" tilewidth="640" tileheight="640" tilecount="6" columns="3">
  <image source="WorldSelectBG03.png" width="1920" height="1280"/>
 </tileset>
 <tileset firstgid="17" name="WorldSelectBG04" tilewidth="640" tileheight="640" tilecount="4" columns="2">
  <image source="WorldSelectBG04.png" width="1280" height="1280"/>
 </tileset>
 <objectgroup name="GameSelectButtons">
  <object id="1" name="gd0" x="390" y="11424"/>
  <object id="5" name="gd1" x="247" y="11369"/>
  <object id="6" name="gd2" x="136" y="11229"/>
  <object id="7" name="gd3" x="223" y="11088"/>
  <object id="8" name="gd4" x="358" y="11023"/>
  <object id="9" name="gd5" x="495" y="10922"/>
  <object id="10" name="gd6" x="454" y="10767"/>
  <object id="11" name="gd7" x="332" y="10699"/>
  <object id="12" name="gd8" x="237" y="10593"/>
  <object id="13" name="gd9" x="290" y="10481"/>
  <object id="14" name="gd10" x="318" y="10329"/>
 </objectgroup>
 <layer name="Tile Layer 1" width="1" height="18">
  <data encoding="base64" compression="zlib">
   eJxjYCAdMAIxExAzAzELELMCMQAA1AAQ
  </data>
 </layer>
 <layer name="Tile Layer 2" width="1" height="18">
  <data encoding="base64" compression="zlib">
   eJxjYCAM2IGYA4g5gZgLhxoABDAAIw==
  </data>
 </layer>
 <layer name="Tile Layer 3" width="1" height="18">
  <data encoding="base64" compression="zlib">
   eJxjYEAF3EDMA8S8QMwHxPwMxAEADFAAQg==
  </data>
 </layer>
 <layer name="Tile Layer 4" width="1" height="18">
  <data encoding="base64" compression="zlib">
   eJwTZGBgEAJiYSAWYSAfAAATSABL
  </data>
 </layer>
</map>
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.