Move a node to your finger using Swift + SpriteKit

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) {
/* Called before each frame is rendered */
    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) {
    /* Called when a touch begins */
    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)

    //distanceToShip value will eventually be used to figure out when to stop the ship
    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) {
/* Called before each frame is rendered */
    let rate: CGFloat = 0.5; //Controls rate of motion. 1.0 instantaneous, 0.0 none.
    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);

!

+4
3

, : myShip.physicsBody.applyImpluse(vector). , , myShip vector. vector x myShip, , , .. , , update.

, update CGVector, myShip lastTouch, .

- :

var lastTouch: CGPoint? = nil

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    let touch = touches.anyObject() as UITouch
    let touchLocation = touch.locationInNode(self)
    lastTouch = touchLocation
}

override func touchesMoved(touches: NSSet!, withEvent event: UIEvent!) {
    let touch = touches.anyObject() as UITouch
    let touchLocation = touch.locationInNode(self)
    lastTouch = touchLocation
}

// Be sure to clear lastTouch when touches end so that the impulses stop being applies
override func touchesEnded(touches: NSSet!, withEvent event: UIEvent!) {
    lastTouch = nil
}

override func update(currentTime: CFTimeInterval) {
    // Only add an impulse if there a lastTouch stored
    if let touch = lastTouch {
        let impulseVector = CGVector(touch.x - myShip.position.x, 0)
        // If myShip starts moving too fast or too slow, you can multiply impulseVector by a constant or clamp its range
        myShip.physicsBody.applyImpluse(impulseVector)
    }
}

, linearDamping angularDamping myShip.physicsBody. , myShip .

1.0 :

myShip.physicsBody.linearDamping = 1.0
myShip.physicsBody.angularDamping = 1.0

myShip , update:

override func update(currentTime: CFTimeInterval) {
    // Only add an impulse if there a lastTouch stored
    if let touch = lastTouch {
        let impulseVector = CGVector(touch.x - myShip.position.x, 0)
        // If myShip starts moving too fast or too slow, you can multiply impulseVector by a constant or clamp its range
        myShip.physicsBody.applyImpluse(impulseVector)
    } else if !myShip.physicsBody.resting {
        // Adjust the -0.5 constant accordingly
        let impulseVector = CGVector(myShip.physicsBody.velocity.dx * -0.5, 0)
        myShip.physicsBody.applyImpulse(impulseVector)
    }
}
+3

2017 , .

, ...

  override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {

    let t: UITouch = touches.first! as UITouch

    let l =      t.location(in: parent!)
    let prev =   t.previousLocation(in: parent!)

    let delta = (l - prev).vector

    physicsBody!.applyImpulse(delta)
 }

.


. (A) , - deltaTime, . " 100", . (B) , , , CGPoint CGVector, - .

+2

thuchesBegan touchesMoved "". update reset xVelocity 0, / .

x, touchLocation.x. , , . , , , touchMoved.

touchesMoved shipLeft, touchesBegan. , .

-1

All Articles