Daniel Mihaila
Daniel Mihaila

Reputation: 585

moving sprite based on drag length/direction

In my game I have a node on the bottom of the screen that id like to move along the x axis using touch. I'd like for my node to move left or right depending on the direction of the drag, and to also move the same distance as the drag. So if the user drags from left to right (CGPoint(x: 200, y: 500)toCGPoint(x:300, y: 500)) the node would move 100 to the right. This is what I've tried to do, but it didn't work. If anyone has a way to fix this I'd really appreciate it

 override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {

        let touch = touches.first as! UITouch
        let touchLocation = touch.locationInNode(self)
        firstTouch = touchLocation

 override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
        let touch = touches.first as! UITouch
        let touchLocation = touch.locationInNode(self)
        secondTouch = touchLocation

        if gameStarted {

            let change = secondTouch.x - firstTouch.x
            let move = SKAction.moveToX(greenGuy.position.x + change, duration: 0.1)

            greenGuy.runAction(move)     
    }
}

Upvotes: 0

Views: 292

Answers (2)

MaxKargin
MaxKargin

Reputation: 1545

You have a pretty good start. First, change:

let move = SKAction.moveToX(greenGuy.position.x + change, duration: 0.1)

to:

let move = SKAction.moveByX(greenGuy.position.x + changeInX, duration: moveDuration)

If you want 2-dimensional movement, use the SKAction moveByX:ChangeInX y:ChangeInY duration:moveDuration instead. Now, you have a few more variables that are based on the duration/distance of the swipe. The duration that you will choose for moveDuration will be up to you, and it will be the product of some coefficient and the swipe distance.

To get the swipe distance: Id advise you to ditch the touches method and use a UIGestureRecognizer. The one you need is UIPanGestureRecognizer. Here's a useful link detailing its use: UISwipeGestureRecognizer Swipe length . Essentially it has different states that get set when the user starts or ends a swipe/drag motion. Then, you could take the locationInView at those moments and calculate the distance between them :D

Hope I helped. I know touchesMoved is also a way to do it, but Ive had problems with it in the past (unnecessary lagging and uncertainty) and gesture recognizers are much more intuitive and easier to use.

Upvotes: 0

aramusss
aramusss

Reputation: 2401

Update your touchesMoved with the following code:

let touch = touches.first as! UITouch
let touchLocation = touch.locationInNode(self)
secondTouch = touchLocation

if gameStarted { 
   let change = secondTouch.x - firstTouch.x
   //Update greenGuys position
   greenGuy.position = CGPoint(x: greenGuy.position.x + change, y:greenGuy.position.y)
   //Update the firstTouch
   firstTouch = secondTouch 
}

The reason not using an SKAction as I commented before is because we don't know how much time will pass between two calls of touchesMoved method, so we don't know exactly what time to input in SKAction duration.

Upvotes: 1

Related Questions