keloa
keloa

Reputation: 115

Using pan gesture recognizer in SpriteKit

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.

Upvotes: 2

Views: 1222

Answers (1)

peacetype
peacetype

Reputation: 2068

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.
}

Upvotes: 4

Related Questions