I'm looking for help on a problem I've been stuck on for a while but cannot find an answer to.
I have a SpriteKit kids game app in which the user can drag objects (SKspriteNodes) around the screen. This is controlled in the "touches" events like so:
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
super.touchesBegan(touches, withEvent: event)
let touch = touches.first as! UITouch
let touchLocation = touch.locationInNode(self)
if piece01.containsPoint(touchLocation) && piece01Placed == false && aPieceIsBeingGrabbed == false {
piece01Grabbed = true
aPieceIsBeingGrabbed = true
self.piece01.zPosition = 4.0
}
}
override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
super.touchesMoved(touches, withEvent: event)
let touch = touches.first as! UITouch
let touchLocation = touch.locationInNode(self)
if piece01Grabbed && piece01Placed == false {
self.piece01.position = CGPointMake(touchLocation.x + worldScale * 120, touchLocation.y - worldScale * 80)
}
}
My problem is that if the user touches another part of the screen while dragging a sprite, a glitch occurs and the Sprite bounces back and forth between the two touch locations and can't decide which one to stay with.
Does anyone know how I can prevent this so that the Sprite always follows the touch that it was initially grabbed by? Thanks in advance!
for touch in touches { let thisTouch = touch as! UITouch;}. UITouch objects are persistent with multi-touch sequences. You can read about UITouch objects more here - Mason