Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CCMoveTo: speed of move

I have some problem with CCMoveTo:

id actionMove = [CCMoveTo actionWithDuration:3 position:ccp(pointBoard[x][y].x,pointBoard[x][y].y)];

for example my sprite start move from ccp(20,460) and move to ccp(20,0) it's ok. But when sprite need to move to ccp(20,200) than movement speed become slower. I need to move sprite with the same speed. How can i do it?

Thanks.

like image 369
Mr.X Avatar asked May 13 '11 06:05

Mr.X


2 Answers

You need to calculate the 'distance' between your [start] and [end] points and then you can calculate the 'duration' so that your sprite moves with constant speed. Something like,

float speed = 1; // here you define the speed that you want to use.

CGPoint start = sprite.position; // here you will get the current position of your sprite.
CGPoint end = ccp(pointBoard[x][y].x,pointBoard[x][y].y);

float distance = ccpDistance(start, end); // now you have the distance

float duration = distance/speed;  // here you find the duration required to cover the distance at constant speed

Now you can call the CCMoveTo function and provide above calculated duration to make your sprite move at same speed.

Hope it helps..!!

like image 188
Tayyab Avatar answered Nov 04 '22 18:11

Tayyab


To keep your movement speed constant for all distances, define a speed you need to move the sprite with and use the speed-time-distance formula you once learned as a child in your physics class to find an unknown from the three.

float speed = 50.0f;
id duration = ccpDistance(sprite.position, pointBoard[x][y]) / speed;
id moveAction = [CCMoveTo actionWithDuration:duration position:pointBoard[x][y]];
like image 44
Mubeen Iqbal Avatar answered Nov 04 '22 20:11

Mubeen Iqbal