Tile Map Ports?

Hey all, avid user and fan of Cocos2d for the iPhone, and decided to try my hand at moving some stuff to the Windows platform for fun.

Had a quick question in regards to using an Object Group form a TMX map, I cannot figure out the proper way to go about this, and it’s been much too long since using template classes and structs…

The Mac/Obj-C code for loading the object group I had is as follows:

CCTMXObjectGroup *objects = [tileMap objectGroupNamed:@"Objects"];
NSAssert(objects != nil, @"'Objects' object group not found");
NSMutableDictionary *spawnPoint = [objects objectNamed:@"SpawnPoint"];
NSAssert(spawnPoint != nil, @"Spawn Point not Found!");

It was straightforward enough until the NSMutableDictionary, I am aware that anything with the NS prefix becomes CC, however I can’t figure out how to get the CCDictionary to work… I’m sure I’m missing something simple, but I’m lost as of now lol…

c++ code up to now:

CCTMXObjectGroup *objects = tileMap->objectGroupNamed("Objects");
CCAssert(objects != NULL, "'Objects' object group not found");
//CCDictionary* spawnPoint = objects->objectNamed("SpawnPoint");
//CCAssert(spawnPoint != NULL, "Spawn Point not Found!");

My failed attempt at using the dictionary… was hopeful, but oh well :wink: Any help would be appreciated, I know I’m probably missing something simple probably, but thank you in advance!

Alrighty, so I dug up the test programs (totally forgot they were included :P) and got a little bit further… But am brickwalling again… Maybe if I post here, 5 hours later I’ll magically get another step further lol

So, now my issue is accessing a single object in the object group. My TMX file contains an object layer that has: NPC spawns, shop locations , and the player’s spawn point.
I’ve got the player spawning at a spawn point now, however it is not THE spawn point object I set, and instead he spawns in the first NPC location. Below is the code for the dictionary

CCTMXObjectGroup *objects = tileMap->objectGroupNamed("Objects");
        CCAssert(objects != NULL, "'Objects' object group not found");
        CCMutableArray *objects = group->getObjects();
        //CCMutableArray *spawnPoint = objects->objectNamed("SpawnPoint");

        CCStringToStringDictionary* dict;
        CCMutableArray::CCMutableArrayIterator it;

        int x, y;

        for( it = spawnPoint->begin(); it != spawnPoint->end(); it++) 
        {
            dict = (*it);//dynamic_cast(*it);

            if(!dict)
                break;
            x = dict->objectForKey("x")->toInt();
            y = dict->objectForKey("y")->toInt();
        }

Now I know why he is spawning in the first NPC location, however I don’t know how to access the single “SpawnPoint” object. You can see the commented code, however it says that it cannot be used to initialize the entity. How unfortunate…

Again, any help, tips, or insight would be greatly appreciated… thanks all!

Bryan Taylor

Honestly, I hate CCMutableArray, CCArray, CCMutalbeDictionary, too. They’re not as elegant as I wish. But there seems no better choice.

To your problem, the return value of CCTMXObjectGroup::objectNamed is a CCStringToStringDictionary* type, and CCStringToStringDictionary is defined to

    typedef CCDictionary CCStringToStringDictionary;

It’s different from CCDictionary<std::string, CCObject*>, that’s your error caused.

cocos2d-x use CCString to implement CCString::toInt(), CCString::toFloat() instead of [NSString intValue], [NSString floatValue]

Your code

CCTMXObjectGroup *objects = [tileMap objectGroupNamed:@"Objects"];
NSAssert(objects != nil, @"'Objects' object group not found");
NSMutableDictionary *spawnPoint = [objects objectNamed:@"SpawnPoint"];
NSAssert(spawnPoint != nil, @"Spawn Point not Found!");

can be translated to

CCTMXObjectGroup *objects = tileMap->objectGroupNamed("Objects");
CCAssert(objects != NULL, "'Objects' object group not found");
CCStringToStringDictionary* spawnPoint = objects->objectNamed("SpawnPoint");
CCAssert(spawnPoint != NULL, "Spawn Point not Found!");

int x = spawnPoint->objectForKey("x")->toInt();
int y = spawnPoint->objectForKey("y")->toInt();

Have a try.

Yep, just as Walzer wrote above, you could get the “spawn point” object by its name you defined in the tmx file.
But it seems that you know how to get the object in your code, hope that the method above could resolve your problem.

Yup that did it! :smiley:

Thanks for the help, I knew I was missing something small… I appreciate the help guys, very much appreciated!

Bryan Taylor

Help me fix this code. Thanks you.