Help converting Ray Wenderlich's Objective C to C++ for Windows

Hi,

I was just following Ray Wenderlich’s tutorial here:
http://www.raywenderlich.com/1163/how-to-make-a-tile-based-game-with-cocos2d

I’m using C*+ on Windows to make a “Farmville” type game so I have to somehow convert all of his code to C*+. I’m using Cocos2dx v2.0-2.04. The part where I am stuck is

CCTMXObjectGroup *objects = [_tileMap objectGroupNamed:@"Objects"];
NSAssert(objects != nil, @"'Objects' object group not found");
NSMutableDictionary *spawnPoint = [objects objectNamed:@"SpawnPoint"];        
NSAssert(spawnPoint != nil, @"SpawnPoint object not found");
int x = [[spawnPoint valueForKey:@"x"] intValue];
int y = [[spawnPoint valueForKey:@"y"] intValue];

and my converted code is

CCTMXObjectGroup* objects = tileMap->objectGroupNamed("Objects");
CC_BREAK_IF(! objects);
CCDictionary* spawnPoint = objects->objectNamed("SpawnPoint");
CC_BREAK_IF(! spawnpoint);

int x = spawnPoint->valueForKey("x");
int y = spawnPoint->valueForKey("y");

The part with valueForKey for x and y shows the error of:
Error: a value of type “const cocos2d::CCString *” cannot be used to initialize an entity of type “int”

Could someone tell me what went wrong?

I think the problem is in the following lines:
int x = spawnPoint~~>valueForKey;
int y = spawnPoint~~>valueForKey(“y”);

Shouldn’t this be something like spawnPoint->intValueForKey(“x”) ?
I can’t check the correct syntax as i’m not working on my dev machine at the moment…

Thanks for the reply!

Well,

spawnPoint->valueForKey("x");
spawnPoint->valueForKey("y");

will both not show any errors, but for the next part of the tutorial, Ray writes:

self.player = [CCSprite spriteWithFile:@"Player.png"];
_player.position = ccp(x, y);
[self addChild:_player]; 

so will it be:

this->player = CCSprite::create("Player.png");
player->setPosition(x,y);
this->addChild(player);

This will show that x and y are undefined…

John Teddy wrote:

Thanks for the reply!
>
The value for key returns an CCString pointer so that the error you are getting:

const CCString* valueForKey (const std::string & key)

CCString has a method to convert the string to the int value you need.

The following should convert it to int values:

int x = spawnPoint~~>valueForKey~~>intValue;
int y = spawnPoint~~>valueForKey~~>intValue;
Also i think
player~~>setPosition;
should be
player~~>setPosition(ccp(x,y));

Well,
>
[…]
>
will both not show any errors, but for the next part of the tutorial, Ray writes:
>
[…]
>
so will it be:
>
[…]
>
This will show that x and y are undefined…

Thanks Edwin.

That really did the trick. Cocos2dx is hard for a C++ user when a lot of the tutorials are in Objective C. :slight_smile:

John Teddy wrote:

Thanks Edwin.
>
That really did the trick. Cocos2dx is hard for a C*+ user when a lot of the tutorials are in Objective C. :slight_smile:
No thanks, glad to be of help :slight_smile:
Most of the difference is in the syntax of the languages , i had the same problems when converting my code from objective-c to c*+.