UPDATE: I solved the problem and figured out a simpler way to do this, then provided an answer. My solution was to speed SPACESHIP equal to the distance that was from my finger touch. For faster movement, you can multiply this speed by a constant. In this case, I used 16. I also got rid of setting lastTouch to zero in the touchs.End event. Thus, the ship will still stop, even when I release my finger.
override func update(currentTime: CFTimeInterval) {
if let touch = lastTouch {
myShip.physicsBody.velocity = CGVector(dx: (lastTouch!.x - myShip.position.x) * 16, dy: 0)
}
}
=================================
I have a SPACESHIP node with X-Axis restricted motion. When the user CLICK and CHECK somewhere on the screen, I want SPACESHIP to be able to move to the x-coordinate of the finger and not stop moving toward the finger until the finger is released. If SPACESHIP is close to the user's finger and the user's finger is still pressed, I want it to slowly slow down and stop. I also want this smooth motion to apply when SPACESHIP changes direction, starts and stops.
I am trying to find a better way to do this.
node, , : , . , . , , , ,
SPACESHIP node, , , , , .
:
1:. , , , myShip (SPACESHIP),
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
let touch = touches.anyObject() as UITouch
let touchLocation = touch.locationInNode(self)
if (touchLocation.x < myShip.position.x) {
myShip.xVelocity = -200
} else {
myShip.xVelocity = 200
}
}
2. , , , . , .
override func touchesMoved(touches: NSSet!, withEvent event: UIEvent!) {
let touch = touches.anyObject() as UITouch
let touchLocation = touch.locationInNode(self)
let xDist: CGFloat = (touchLocation.x - myShip.position.x)
let yDist: CGFloat = (touchLocation.y - myShip.position.y)
let distanceToShip: CGFloat = sqrt((xDist * xDist) + (yDist * yDist))
if (myShip.position.x < touchLocation.x) && (shipLeft == false) {
shipLeft = true
myShip.xVelocity = 200
}
if (myShip.position.x > touchLocation.x) && (shipLeft == true) {
shipLeft = false
myShip.xVelocity = -200
}
}
3 , , .
override func touchesEnded(touches: NSSet!, withEvent event: UIEvent!) {
myShip.xVelocity = 0
}
4 ,
override func update(currentTime: CFTimeInterval) {
let rate: CGFloat = 0.5;
let relativeVelocity: CGVector = CGVector(dx:myShip.xVelocity - myShip.physicsBody.velocity.dx, dy:0);
myShip.physicsBody.velocity = CGVector(dx:myShip.physicsBody.velocity.dx + relativeVelocity.dx*rate, dy:0);
!