iosswiftsprite-kitskspritenodeskcameranode

Can setScale() on an SKCameraNode exclude certain children?


I have an SKCameraNode that has been added to my SKScene as a child. I have a pinch gesture recognizer that scales a scene using the gesture recognizer's scale property in combination with the SKCameraNode's setScale method.

class GameScene: SKScene 
...
override func didMove(to view: SKView) {
   let cameraNode = SKCameraNode()
   cameraNode.position = self.midPoint
   self.addChild(cameraNode)
   self.camera = cameraNode
        
   let pinchGesture = UIPinchGestureRecognizer()
   pinchGesture.addTarget(self, action: #selector(scale(_:)))
   view.addGestureRecognizer(pinchGesture)

...

@objc func scale(_ sender: UIPinchGestureRecognizer) {
    guard let camera = self.camera, (sender.state == .began || sender.state == .changed) else {
        print("GameScene - Pinch Gesture Action - No camera for scene")
        return
    }
    
    let scale = 1 / sender.scale
    let newScale = max(0.5, min(1.0, previousCameraScale * scale))
    previousCameraScale = newScale
    camera.setScale(newScale)
}

My question - I want only SOME of the children of GameScene to scale, but not all of them. Is there a way I can specify which ones I want to scale?


Solution

  • You can add the nodes that you don't want to scale as child nodes to the camera.

    See the Getting Started with a Camera documentation:

    A camera’s descendants are always rendered relative to the camera node’s origin and without applying the camera’s scaling or rotation to them. For example, if your app displays scores or other data floating above the gameplay, the nodes that render these elements should be added as child nodes to the camera.