Moving an object across the screen at a certain speed.(Sprite Kit)

戏子无情 提交于 2019-12-02 06:03:41

I think you could revise your approach based on the calculation speed. So, if you know your speed, you can calculate how many time a node takes to arrive to a point and change your speed to a reasonable value.

// #-#-#-#-#-#-#-#-#-#-#-#-#-#-#
//MARK: - Calculate action duration btw two points and speed
// #-#-#-#-#-#-#-#-#-#-#-#-#-#-#
func getDuration(pointA:CGPoint,pointB:CGPoint,speed:CGFloat)->NSTimeInterval {
    let xDist = (pointB.x - pointA.x)
    let yDist = (pointB.y - pointA.y)
    let distance = sqrt((xDist * xDist) + (yDist * yDist));
    let duration : NSTimeInterval = NSTimeInterval(distance/speed)
    return duration
}

Suppose you have your node with a speed of 150.0

Your move will be:

let moveObject = SKAction.moveTo(endpoint, duration: getDuration(node.position,pointB:endpoint,speed:150.0))

With this approach you speed dont' change and you don't have this disagreeable slowness.

P.S. Don't forgot to change the uppercase to your properties: in swift is a bad attitude, use object instead of Object.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!