1

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)
    }
}
Meet Doshi
  • 4,241
  • 10
  • 40
  • 81
james
  • 69
  • 5

1 Answers1

2

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.

Alessandro Ornano
  • 34,887
  • 11
  • 106
  • 133
  • @AlessandroOmano I did this and its solved my problem. Thanks. But it created a weird glitch. The glitch is very hard to explain so Im going to attach the link to a video that shows the glitch happening. I will also include the necessary code. Here is the link to a video of the glitch happening:https://www.youtube.com/watch?v=jHE5RC-mvwU The glitch did not occur before. Do you know why its happening and if so what do I need to do the stop it? I will also add the new code to the original post. – james Aug 01 '16 at 04:18
  • I've seen both video and your code. I've this kind of experience when I had used giant values like you. If you now that for example your top is equal to self.frame.height-20, to hidden the ball try to use an endpoit equal to for example self.frame.height+40, don't use giant values because you could have the typical glitch type of the video, same thing applied to the bottom – Alessandro Ornano Aug 01 '16 at 05:51
  • Ok. When you say "giant values" do you mean the endpoints. If so I changed the endpoint to self.frame.height+40 and the glitch still happened. – james Aug 01 '16 at 07:12
  • Oh yes, you have reason but I've see now in your code another thing...why you don't stop your action when you start another moveTo : this is another thing that cause this glitch, the best way to do is to give a withKey to your action, check if it's running with .actionForKeyIsRunning("moveToUpThisBall") and stop it with removeActionForKey – Alessandro Ornano Aug 01 '16 at 07:14
  • What I mean is: everytime you have a runAction, use withKey to have a reference to your action ;) let me know your progress – Alessandro Ornano Aug 01 '16 at 07:18
  • You wont believe me. But I just thought of that after I finished writing the previous post. I then looked up some ways to remove the moving down action. Then I added fishHook.removeAllActions to the function that moves the fish up. And it worked. The other tutorial talked about the ForKey but it worked fine when I just did remove all actions from the object. Is one way better? – james Aug 01 '16 at 07:25
  • Oh nice work man, these are good news, very well. The best way to do it's to give to an action a key and analize the situation everytime you want to start a new action. You could have for example a rotation or another move that you dont want to stop, and removeAllAction stop all so it's not a good way. – Alessandro Ornano Aug 01 '16 at 07:26
  • Ok that makes sense. And thanks again for your help. – james Aug 01 '16 at 07:30