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?
this touch code should work for you
extension GameScene {
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches{
let location = touch.location(in: self)
let _ = self.nodes(at: location).map { ($0 as? DraggableNode)?.isPicked = true }
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches{
let touchLoc = touch.location(in: self)
let prevTouchLoc = touch.previousLocation(in: self)
let picked_nodes = self.children.filter { ($0 as? DraggableNode)?.isPicked ?? false }
for node in picked_nodes {
let deltaX = touchLoc.x - prevTouchLoc.x
let deltaY = touchLoc.y - prevTouchLoc.y
node.position.x += deltaX
node.position.y += deltaY
}
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
let _ = self.children.map { ($0 as? DraggableNode)?.isPicked = false }
}
}
combined with the following SKScene. i would recommend putting your picker flag inside the movable node, that way you can drag individual nodes rather than having a single global test.
class DraggableNode: SKNode {
var isPicked:Bool = false
override init() {
super.init()
let shape = SKShapeNode(circleOfRadius: 50)
shape.fillColor = .red
shape.name = "Selection_Box"
self.addChild(shape)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
class GameScene: SKScene {
let draggableNodeA = DraggableNode()
let draggableNodeB = DraggableNode()
override func didMove(to view: SKView) {
draggableNodeA.position.x = 50
draggableNodeB.position.x = -50
self.addChild(draggableNodeA)
self.addChild(draggableNodeB)
}
}