如何以一致的速度移动IOS SKSprite [英] How do you move an IOS SKSprite at a consistent speed

查看:103
本文介绍了如何以一致的速度移动IOS SKSprite的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在IOS SpriteKit中使用SKSprites,我基本上想让精灵随机移动某个方向一定距离,然后选择一个新的随机方向和距离。很简单,创建一个生成随机的方法数字然后创建动画,并在动画的完成块中回调相同的例程。

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆