开发者

CCMoveTo: speed of move

开发者 https://www.devze.com 2023-03-05 08:51 出处:网络
I have some problem with CCMoveTo: id actionMove = [CCMoveTo act开发者_运维技巧ionWithDuration:3 position:ccp(pointBoard[x][y].x,pointBoard[x][y].y)];

I have some problem with CCMoveTo:

id actionMove = [CCMoveTo act开发者_运维技巧ionWithDuration: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.


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


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


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.


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


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号