我有一个对象需要在屏幕上上下移动。当您第一次单击时,它会在屏幕上向下移动到一个持续时间为 3 秒的端点。您可以随时单击以阻止它向下移动并使其开始向上移动到不同的端点,再次持续 3 秒。这样做的问题是,如果您单击以将对象向下移动,然后立即再次单击以将其向上移动,则对象向上移动但速度非常慢,因为它的持续时间为 3 秒。 (我希望这是有道理的)所以我想要的是停止使用持续时间来设置对象移动的速度/速度。有没有办法说以空白速度移动到 x.y 点?谢谢你。 (无论物体在哪里并且必须移动到哪里,我都希望它始终以相同的速度移动。)
这是我现在用来移动对象的方法:
func moveObject(){
let endpoint = CGPoint(x: self.size.width / 2 , y: self.size.height / 1.8888888888 )
let moveObject = SKAction.moveTo(endpoint, duration: 3.0 )
let moveObjectSequence = SKAction.sequence([moveLine])
Object.runAction(moveLineSequence)
}
修改后的代码:
func dropLine(){
if hookNumber == 1{
let endpoint = CGPoint(x: self.size.width / 2 , y: self.size.height / 1.8888888888 )
let moveLine = SKAction.moveTo(endpoint, duration: getDuration(fishLine.position,pointB:endpoint,speed:300.0))
let moveLineSequence = SKAction.sequence([moveLine])
fishLine.runAction(moveLineSequence)
}
}
func dropHook(){
if hookNumber == 1{
let endpoint = CGPoint(x: self.size.width / 2 , y: self.size.height - 2030)
let moveLine = SKAction.moveTo(endpoint, duration: getDuration(fishHook.position,pointB:endpoint,speed:300.0))
let moveLineSequence = SKAction.sequence([moveLine])
fishHook.runAction(moveLineSequence)
hookNumber = 2
}
}
func raiseLine(){
if hookNumber == 2{
let endpoint = CGPoint(x: self.size.width / 2 , y: 3050 )
let moveLine = SKAction.moveTo(endpoint, duration: getDuration(fishLine.position,pointB:endpoint,speed:300.0))
let moveLineSequence = SKAction.sequence([moveLine])
fishLine.runAction(moveLineSequence)
}
}
func raiseHook(){
if hookNumber == 2{
let endpoint = CGPoint(x: self.size.width / 2 , y: self.size.height - 3 )
let moveLine = SKAction.moveTo(endpoint, duration: getDuration(fishHook.position,pointB:endpoint,speed:300.0))
let moveLineSequence = SKAction.sequence([moveLine])
fishHook.runAction(moveLineSequence)
}
}
最佳答案
我认为您可以根据计算速度修改您的方法。 所以,如果你知道你的速度,你可以计算一个节点到达一个点需要多少时间,并将你的速度更改为一个合理的值。
// #-#-#-#-#-#-#-#-#-#-#-#-#-#-#
//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
}
假设您的节点速度为 150.0
您的举动将是:
let moveObject = SKAction.moveTo(endpoint, duration: getDuration(node.position,pointB:endpoint,speed:150.0))
通过这种方法,您的速度不会改变,也不会出现这种令人不快的缓慢。
附言不要忘记将大写更改为您的属性:在 swift 中是一种不好的态度,请使用 object 而不是 Object。
关于swift - 以一定的速度在屏幕上移动一个对象。(Sprite Kit),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38681205/