iosswiftsprite-kittouchesbegantouchesmoved

Moving SpriteKit Node with Touch Gestures only moves one point with each new touch


I have a SpriteKitNode that I want to move around my scene when I touch it and drag it.

I have a touchesBegan method that detects if the particular node I want to move is being touched:

var selectionBoxIsTouched: Bool!
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        for touch in touches{
            let location = touch.location(in: self.canvasScene)
            canvasScene.enumerateChildNodes(withName: "Selection_Box", using:
                                                { [self] (node, stop) -> Void in
                if self.canvasScene.atPoint(location) == node {
                    selectionBoxIsTouched = true
                } else {
                    selectionBoxIsTouched = false
                }
            })
        }
    } 

Next, I have touchesMoved method where if my node is currently being touched, it gets moved as the user moves a finger across the screen:

  
    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        if selectionBoxIsTouched {
            if let touch = touches.first {
                let touchLoc = touch.location(in: self.canvasScene)
                let prevTouchLoc = touch.previousLocation(in: self.canvasScene)
                
                canvasScene.enumerateChildNodes(withName: "Selection_Box", using:
                                                    { (node, stop) -> Void in
                    if let touchedNode = node as? SKShapeNode {
                        let newYPos = touchedNode.position.y + (touchLoc.y - prevTouchLoc.y)
                        let newXPos = touchedNode.position.x + (touchLoc.x - prevTouchLoc.x)
                        touchedNode.position = CGPoint(x: newXPos, y: newYPos)  //set new X and Y for your sprite.
                    }
                })
            }
        }
    }

When I select the node + try to move it, it doesn't move in a continuous motion....it moves a small amount, then stops; even if my finger keeps moving across the screen.

How do I fix this so that the node moves in a smooth, continuous motion with my finger?


Solution

  • The issue ended up being that I had a pan gesture recognizer that was conflicting with the multitouch delegate methods. I fixed by adding logic to enable/disable that gesture recognizer if the particular node I wanted to move was being touched or not.