views:

657

answers:

1

Im trying to move an object around the screen like in the game geometry wars. I can rotate the object just fine, however I cant seem to get it to move based on the direction it is facing. I have this code here which i think is right for doing this but I keep getting syntax errors:

spriteObject.x = spriteObject.x + speed*cos(Angle)
spriteObject.y = spriteObject.y + speed*sin(Angle)

The errors are 'request for member x not in struct or union.' How do you do this in Objective-c/cocos2d syntax?

+1  A: 

Looking at the documentation for the sprite class, you would need to do the following:

float angle = spriteObject.rotation
spriteObject.position.x = spriteObject.position.x + speed*cos(angle)
spriteObject.position.y = spriteObject.position.y + speed*sin(angle)

edit (in response to comment):

I see that you are programming for the iPhone, which means you need to be using the iphone cocos2d library, and not the one I linked to before.

The syntax will be different, as will the example code, since the iPhone version uses the Objective-C langugage, whereas the original cocos2d uses Python.

Google code has good documentation on the iPhone version of cocos2d, including sample code.

Based on that sample code, you will have to do the following:

float newX = spriteObject.position.x + speed * cos(angle);
float newY = spriteObject.position.y + speed * sin(angle);
spriteObject.position =  ccp( newX, newY );
e.James
using that gives me the same errors...spriteObject.position.x = spriteObject.x + speed*cos(Angle)spriteObject.position.y = spriteObject.y + speed*sin(Angle)
Jack Null