I have an object that needs to move up and down the screen. When you first click it moves down the screen to an endpoint with a duration of 3 seconds. You can click at anytime to stop it from moving down and make it start moving up to a different endpoint, again at a duration of 3 seconds. The problem with doing it this way is if you click to move the object down but then immediately click again to move it up, the object moves up but at a very slow rate because it has a duration of 3 seconds. (I hope this makes sense) So what I want is to stop using the duration to set the pace/speed at which the object moves. Is there a way to say move to point x.y at blank speed? Thank you. (No matter where the object is and has to move to I want it to always move at the same pace.)
This is what I use to move the object right now:
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)
}
Code after corrections:
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)
}
}
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.
来源:https://stackoverflow.com/questions/38681205/moving-an-object-across-the-screen-at-a-certain-speed-sprite-kit