Mr.X
Mr.X

Reputation: 71

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.

Upvotes: 6

Views: 8616

Answers (5)

P.J.Radadiya
P.J.Radadiya

Reputation: 1541

You Can spped mantain of speed variable and Position mainatain position:CGPointMake action.

float speed = 3.67;

CCMoveTo *moveuserleft; CCMoveTo *moveuserleft2;

    moveuserleft = [CCMoveTo actionWithDuration:speed position:CGPointMake(235*scaleX,200*scaleY)];

    moveuserleft2 = [CCMoveTo actionWithDuration:speed  position:CGPointMake(360*scaleX,200*scaleY)];

    CCSequence *scaleSeqleft = [CCSequence actions:moveuserleft,moveuserleft2, nil];

    [user runAction:scaleSeqleft];

Upvotes: 0

Mubeen Iqbal
Mubeen Iqbal

Reputation: 188

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]];

Upvotes: 5

zoom8amit
zoom8amit

Reputation: 332

Just use simple maths (time = distance/ speed) to calculate the time required for moveAction.

float speed = 13.0;
CGPoint startPoint = ccp(20,300);
CGPoint endPoint   = ccp(20,100);

float time = ccpDistance(startPoint, endPoint) / speed; 
id moveAction = [CCMoveTo actionWithDuration:time position:endPoint];

Upvotes: 0

Tayyab
Tayyab

Reputation: 10631

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..!!

Upvotes: 19

Anish
Anish

Reputation: 2917

Here the speed of the sprite varies based on the distance.if the distance from ccp(20,460) to ccp(20,0) is same as ccp(20,0) to ccp(20,200).Speed remains same.But if the distance varies the speed varies accordingly(if the duration is same).

You can reduce the time if u want more speed.

Upvotes: 0

Related Questions