I am new to Cocos2D-X and have been trying to learn it for the past couple months now. I am trying to create a sample game from the book Cocos2d-x Game Development Blueprints and in this particular chapter it's explaining how to create a game using tiled map.
I Need help parsing a list of objects from a TMX file, I am trying to update the code from version 2.X to version 3.X, because I get compile errors. I need to change the deprecated CCArray and CCDictionary into the new cocos::Vector and Map in the following code
void GameWorld::CreateTiledMap()
{
// generate level filename
char buf[128] = {0};
sprintf(buf, "level_%02d.tmx", GameGlobals::level_number_);
// create & add the tiled map
tiled_map_ = CCTMXTiledMap::create(buf);
addChild(tiled_map_);
// get the size of the tiled map
columns_ = (int)tiled_map_->getMapSize().width;
rows_ = (int)tiled_map_->getMapSize().height;
// save a reference to the layer containing all the bricks
bricks_layer_ = tiled_map_->layerNamed("Bricks");
// parse the list of objects
CCTMXObjectGroup* object_group = tiled_map_->objectGroupNamed("Objects");
CCArray* objects = object_group->getObjects();
int num_objects = objects->count();
for(int i = 0; i < num_objects; ++i)
{
CCDictionary* object = (CCDictionary*)(objects->objectAtIndex(i));
// create the Hero at this spawning point
if(strcmp(object->valueForKey("name")->getCString(), "HeroSpawnPoint") == 0)
{
CreateHero(ccp(object->valueForKey("x")->floatValue(), object->valueForKey("y")->floatValue()));
}
// create an Enemy at this spawning point
else if(strcmp(object->valueForKey("name")->getCString(), "EnemySpawnPoint") == 0)
{
CCPoint position = ccp(object->valueForKey("x")->floatValue(), object->valueForKey("y")->floatValue());
CCPoint speed = ccp(object->valueForKey("speed_x")->floatValue(), object->valueForKey("speed_y")->floatValue());
CreateEnemy(position, speed);
}
// create a Platform at this spawning point
else if(strcmp(object->valueForKey("name")->getCString(), "PlatformSpawnPoint") == 0)
{
CCPoint position = ccp(object->valueForKey("x")->floatValue(), object->valueForKey("y")->floatValue());
CCPoint speed = ccp(object->valueForKey("speed_x")->floatValue(), object->valueForKey("speed_y")->floatValue());
CreatePlatform(position, speed);
}
// save the point where the level should complete
else if(strcmp(object->valueForKey("name")->getCString(), "LevelCompletePoint") == 0)
{
level_complete_height_ = object->valueForKey("y")->floatValue();
}
}
}
I have tried converting it myself, but have been unsuccessful. I am having trouble converting the CCArray to cocos::Vector, but mainly changing the CCDictionary to cocos::Map. Can someone Please help me?
Cocos2d-x has many tests, which is very helpful. From \tests\cpp-tests\Classes\TileMapTest\TileMapTest.cpp
:
auto map = TMXTiledMap::create("TileMaps/test-object-layer.tmx");
addChild(map, -1, kTagTileMap);
CCLOG("----> Iterating over all the group objets");
auto group = map->getObjectGroup("Objects");
auto& objects = group->getObjects();
for (auto& obj : objects)
{
ValueMap& dict = obj.asValueMap();
float x = dict["x"].asFloat();
string name = dict["name"].asString();
}