问题描述
在IOS SpriteKit中玩SKSprites,我基本上想让一个精灵随机移动某个方向一定距离,然后选择一个新的随机方向和距离.很简单,创建一个生成随机的方法numbers 然后创建动画,并在动画的完成块中回调相同的例程.
Playing around with SKSprites in the IOS SpriteKit, and I basically want to have a sprite randomly move a certain direction for a certain distance, then pick a new random direction and distance.. Simple enough, create a method that generates the random numbers then creates the animation and in the completion block of the animation have it callback the same routine.
这确实有效,但它也可以防止动画以相同的速度移动,因为动画都是基于持续时间的.如果对象必须移动 100,它会以 1/2 的速度移动,如果下一个随机告诉它移动 200... 那么我怎么让它以一致的速度移动呢?
THis does work, but it also keeps the animation from moving at the same velocity since the animations are all based on duration.. If the object has to move 100 it moves at 1/2 the speed it moves if the next random tells it to move 200... So how the heck do I have it move with a consistent speed?
推荐答案
@Noah Witherspoon 在上面是正确的 - 使用 Pythagorus 获得平滑的速度:
@Noah Witherspoon is correct above - use Pythagorus to get a smooth speed:
//the node you want to move
SKNode *node = [self childNodeWithName:@"spaceShipNode"];
//get the distance between the destination position and the node's position
double distance = sqrt(pow((destination.x - node.position.x), 2.0) + pow((destination.y - node.position.y), 2.0));
//calculate your new duration based on the distance
float moveDuration = 0.001*distance;
//move the node
SKAction *move = [SKAction moveTo:CGPointMake(destination.x,destination.y) duration: moveDuration];
[node runAction: move];
这篇关于如何以一致的速度移动 IOS SKSprite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!