I am creating a game in sprite kit using swift, and I am trying to be able to move the SKScene around with a finger because not all of the nodes fit within the scene. I have already created world, overlay, and camera nodes with this code.
override func didMoveToView(view: SKView) {
world = self.childNodeWithName("world")!
if !isCreated {
isCreated = true
// Camera setup
self.anchorPoint = CGPoint(x: 0.5, y: 0.5)
self.world = SKNode()
self.world.name = "world"
addChild(self.world)
self.cam = SKNode()
self.cam.name = "camera"
self.world.addChild(self.cam)
// UI setup
self.overlay = SKNode()
self.overlay.zPosition = 10
self.overlay.name = "overlay"
addChild(self.overlay)
}
I would like to be able to move the camera around by using a pan gesture with a single finger. How would I do this? Any help would be appreciated.
As an alternative to @Kris's solution (which is based in UIKit), you can also monitor touches in Sprite Kit with your SKScene subclass. I wrote a small sample which should point you in the right direction.
class YourSceneSubclass : SKScene
{
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
guard let touch = touches.first else {
return
}
let location = touch.locationInNode(self)
let previousLocation = touch.previousLocationInNode(self)
camera?.position.x += location.x - previousLocation.x
camera?.position.y += location.y - previousLocation.y
}
}
I didn't run this, just wrote it in a playground. Also note that if you want to handle other taps/gestures as well you will have to write additional code making sure the recognition works well for all your intended scenarios.