我对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)比移动速度变慢时.我需要以相同的速度移动精灵.我该怎么做?
谢谢.
Tay*_*yab 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
Run Code Online (Sandbox Code Playgroud)
现在你可以调用CCMoveTo函数并提供上面计算的持续时间,以使你的精灵以相同的速度移动.
希望能帮助到你..!!
要保持所有距离的移动速度不变,请定义移动精灵所需的速度,并使用您曾在物理课中作为孩子学习的速度 - 时间 - 距离公式,以从三者中找到未知.
float speed = 50.0f;
id duration = ccpDistance(sprite.position, pointBoard[x][y]) / speed;
id moveAction = [CCMoveTo actionWithDuration:duration position:pointBoard[x][y]];
Run Code Online (Sandbox Code Playgroud)
这里精灵的速度根据距离而变化。如果从 ccp(20,460) 到 ccp(20,0) 的距离与 ccp(20,0) 到 ccp(20,200) 相同。速度保持不变。但是如果距离变化速度相应变化(如果持续时间相同)。
如果你想要更快的速度,你可以减少时间。