Tiled map not being displayed in a new hello world project

Tiled map not being displayed in a new hello world project
0.0 0

#1

I am trying to load a tiled map to a hello world project. However I see nothing of the tiled on the simulator - this is what the code looks like

Scene* HelloWorld::createScene()
{
    // 'scene' is an autorelease object
    auto scene = Scene::create();
    
    // 'layer' is an autorelease object
    auto layer = HelloWorld::create();

    // add layer as a child to scene
    scene->addChild(layer);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !Layer::init() )
    {
        return false;
    }
	
 
    auto visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();

	
	//Load paths
	std::vector<std::string> searchPaths;
	searchPaths.push_back("/Users/zedan/Development/TestCpp/Resources/content/");
	FileUtils::getInstance()->setSearchPaths(searchPaths);
	
	cocos2d::TMXTiledMap* tileMap;
	tileMap = cocos2d::TMXTiledMap::create("sandbox.tmx");

	//tileMap->setPosition(Vec2(0,0));
	tileMap->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
	this->addChild(tileMap);
	
    return true;
	
}

The tiled map came as an example with the tiled app. I could see the tileMap object has the objectGroups with content from the file however I dont see anything on the screen any suggestions.This is what the object layer content looks like in Tiled

Any suggestions on why I am not seeing anything on the screen ?


#2

For Objects you have to fetch it and have to display manually.
For example:

TMXTiledMap *tileMap = TMXTiledMap::create("1.tmx");
this->addChild(tileMap, 20);

auto group = tileMap->getObjectGroup("Object Layer 1");
auto& objects = group->getObjects();

Value objectsVal = Value(objects);
log("%s", objectsVal.getDescription().c_str());

for (auto& obj : objects)
{
    ValueMap& dict = obj.asValueMap();

    float x = dict["x"].asFloat();
    float y = dict["y"].asFloat();
    float width = dict["width"].asFloat();
    float height = dict["height"].asFloat();
    
    // Add sprites here manually
}

#3

Thanks Ill try this out and report back if it solves the problem.


#4

How do I add obj to this Layer ?


#5

You mean in Tiled editor?
You have to create ObjectLayer by clicking ‘Add Object Layer’ option.
and for code you can check in cpp-test also. TileMapTest


#7

I just expanded on my question here