好的,所以我有一个Cgpoints数组,我将sprite节点移动到迭代数组。距离下一点的精灵距离随每个点而变化
<ul>
<li>
<h2>Lorem ipsum dolor sit</h2>
<span>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam tellus nisl, molestie vitae nibh nec, dictum dignissim dui.Lorem </span>
<button>button</button>
</li>
<li>
<h2>Lorem ipsum dolor sit</h2>
<span>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam tellus nisl, molestie vitae nibh nec, dictum dignissim dui.Lorem </span>
<button>button</button>
</li>
</ul>
我的问题是静态持续时间,具体取决于精灵以更慢/更快的速度移动的位置。无论精灵必须行驶的距离我都需要有恒定的速度。
如何根据精灵与终点的距离改变SKAction速度?
答案 0 :(得分:2)
这需要使用距离公式。现在要保持恒定速度,您需要确定要移动的每秒点数(pps)。
基本上公式变为距离/ pps
所以,假设我们想要旅行(0,0)到(0,300)(这是300分,所以它需要我们2秒)
伪代码:
让持续时间= sqrt((0 - 0)*(0 - 0)+(0 - 300)*(0 - 300))/(150)
持续时间= sqrt(90000)/(150)
持续时间= 300/150
持续时间= 2
然后你会将它弹出到你的移动动作中:
let action = SKAction.move(to: points[i], duration: duration)
您的Swift代码应如下所示:
let pps = 150 //define points per second
func moveGuy() {
var actions = [SKAction]()
var previousPoint = guy.position // let's make sprite the starting point
for point in points
{
//calculate the duration using distance / pps
//pow seems to be slow and some people have created ** operator in response, but will not show that here
let duration = sqrt((point.x - previousPoint.x) * (point.x - previousPoint.x) +
(point.y - previousPoint.y) * (point.y - previousPoint.y)) / pps
//assign the action to the duration
let action = SKAction.move(to: point, duration: duration)
action.timingMode = .easeInEaseOut
actions.append(action)
previousPoint = point
}
guy.run(actions)
}
//Since the entire path is done in moveGuy now, we no longer want to be doing it on update, so instead we do it during the mouse click event
//I do not know OSX, so I do not know how mouse down really works, this may need to be fixed
//Also I am not sure what is suppose to happen if you click mouse twice,
//You will have to handle this because it will run 2 actions at once if not done
override func mouseDown(event:NSEvent)
{
moveGuy()
}
现在你会注意到你的精灵在每次迭代时都在跑步和减速,你可能不希望这样,所以对于你的计时模式,你可能想要这样做
//Use easeInEaseOut on our first move if we are only moving to 1 point
if points.count == 1
{
action.timingMode = .easeInEaseOut
}
//Use easeIn on our first move unless we are only moving to 1 point
else if previousPoint == sprite.position
{
action.timingMode = .easeIn
}
//Use easeOut on our last move unless we are only moving to 1 point
else if point == point.last
{
action.timingMode = .easeOut
}
//Do nothing
else
{
action.timingMode = .linear
}
答案 1 :(得分:0)
您必须创建一个新的SKAction,其持续时间根据要移动的距离和要移动的节点的速度(以点/ s为单位)计算得出。