cocos2d iphone - 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)时,移动速度会变慢。我需要以相同的速度移动精灵。我该怎么做?

谢谢。

你需要计算你的[起点]和[终点]点之间的"距离",然后你可以计算"持续时间",以便你的精灵以恒定的速度移动。像这样,

浮点速度 = 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 函数并提供上述计算的持续时间,以使角色以相同的速度移动。

希望对您有所帮助..!!

为了在所有距离上保持移动速度恒定,请定义移动精灵所需的速度,并使用您小时候在物理课上学过的速度-时间-距离公式从三者中找到未知数。

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

这里精灵的速度根据距离而变化,如果从 ccp(20,460) 到 ccp(20,0) 的距离与 ccp(20,0) 到 ccp(20,200) 的距离相同。速度保持不变。但是,如果距离变化,则速度也会相应变化(如果持续时间相同)。

如果您想要更高的速度,您可以减少时间。

只需使用简单的数学(时间=距离/速度)来计算移动操作所需的时间。

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

您可以保持速度变量和位置维护位置:CGPointMake 动作。

浮子速度 = 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];

最新更新