2011-05-13 39 views
6

的速度,我有一些問題CCMoveTo:CCMoveTo:移動

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

例如從ccp(20,460)我的精靈開始移動,並移動到ccp(20,0)它的確定。但是當精靈需要移動到ccp(20,200)時,移動速度會變慢。 我需要以相同的速度移動精靈。我該怎麼做?

謝謝。

回答

0

這裏,精靈的速度根據距離而變化。如果從ccp(20,460)到ccp(20,0)的距離與ccp(20,0)到ccp(20,200)的距離相同,則速度保持不變。但是如果距離變化,速度會相應變化(如果持續時間相同)。

,可以減少如果u想要更多的速度的時間。

+0

我可以移動精靈沒有持續時間,只需設置速度和位置? – 2011-05-13 07:42:38

+0

如果你不想要持續時間,你可以設置一個持續時間爲0.但是你無法使用MoveTo控制精靈的速度(你可以通過增加它們的位置來手動移動)。 – Anish 2011-05-13 08:07:53

19

你需要計算你的[開始]之間的「距離」和[結束]點,然後就可以計算出「持續時間」,以便以恆定速度你的精靈移動。像,

浮子速度= 1; //在這裏你定義你想要使用的速度。

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 

現在您可以調用CCMoveTo函數並提供以上計算的持續時間,以使您的精靈以相同的速度移動。

希望它可以幫助.. !!

5

爲了讓你的移動速度常數所有距離,確定您需要與移動精靈,並使用你一旦學會在你的物理課一個孩子的速度 - 時間 - 距離公式找到三個未知的速度。

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

速度應該是一個浮動,而不是一個id – apple16 2013-09-02 21:17:59

0

只需使用簡單的數學運算(時間=距離/速度)來計算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

您可以spped mantain的速度變量和位置mainatain位置:CGPointMake動作。

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