CCMoveTo: Movement Speed

I have a problem with CCMoveTo:

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

for example, my sprite launch will move from ccp(20,460) and move to ccp(20,0) . But when the sprite should go to ccp(20,200) , than the speed of movement will become slower. I need to move the sprite at the same speed. How can i do this?

Thanks.

+6
source share
5 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 at a constant speed. Sort of,

float speed = 1; // here you define the speed 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 the calculated duration above so that your sprite moves at the same speed.

Hope this helps .. !!

+19
source

To keep your movement speed constant at all distances, determine the speed with which you need to move the sprite and use the speed-time-distance formula that you once learned in childhood in your physical class to find the unknown of the three.

 float speed = 50.0f; id duration = ccpDistance(sprite.position, pointBoard[x][y]) / speed; id moveAction = [CCMoveTo actionWithDuration:duration position:pointBoard[x][y]]; 
+5
source

Here the speed of the sprite varies with distance. If the distance from ccp (20,460) to ccp (20,0) is the same as ccp (20,0) to ccp (20,200). The speed remains the same. But if the distance changes, the speed changes accordingly (if the duration is the same).

You can reduce the time if you want to increase the speed.

0
source

Just use simple math (time = distance / speed) to calculate the time it takes 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]; 
0
source

You can use variable speed mantain and Position mainatain position: CGPointMake action.

swimming 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]; 
0
source

Source: https://habr.com/ru/post/888000/


All Articles