I'm working on a game using SpriteKit and I want to move some sprites around but I need to move only one sprite at a time. How can I use pan gesture recognizer in SpriteKit? I tried with the normal way and I got some errors so I thought maybe it has a special way.
1 Answers
4
votes
To add a pan gesture recognizer in your game, add the gesture recognizer in your GameScene's didMove method. Then add a new function (handlePanFrom, below) in your GameScene file which will be called when the gesture is recognized.
override func didMove(to view: SKView) {
// Create the gesture recognizer for panning
let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(handlePanFrom))
// Add the gesture recognizer to the scene's view
self.view!.addGestureRecognizer(panGestureRecognizer)
}
@objc func handlePanFrom(_ recognizer: UIPanGestureRecognizer) {
// This function is called when a pan gesture is recognized. Respond with code here.
}