3

My goal is to have a static node in the background of the SKScene. In this scene, I use SKCameraNode to pan around as needed.

The background node has to be static because it presents the sky in far distance which is expected not to move.

Problem:

If I would simply add the background node as a child to the scene, I would have to move the background in the same way the camera is panned, in order to have background always fully visible inside camera's viewport. I don't want to do that because the background node has some visual effects and filters applied to it and therefore moving it causes performance drop.

What I want, is having background laid out in the background as a static node or view, which would not have to be moved in order to "follow" the camera.

What have I tried:

I know that a node can be added as a child to SKCameraNode in order to make sure it follows the camera. But as Apple suggests, this is useful for HUD elements like score labels, etc., which are usually rendered above everything. This is of course not applicable if you want to add background, since I want it to be in the background, behind the nodes that are added as child nodes directly to the scene.

Even though, I tried adding background node as a child to camera node and play with z position and things like that, but nothing helped, as expected.

I also tried adding background as a subview to root SKView but I wasn't able to get it rendered behind the scene for some reason.

Thanks in advance.

damirstuhec
  • 6,069
  • 1
  • 22
  • 39

1 Answers1

4

Edit: Never mind my first answer was a dead end. The following works though and is seems to be something you've already attempted: Adding the background as a child to the camera.

I guess you've just have been having some problems with the zPosition. The below proof of concept works as intended as far as I can tell.

class GameScene: SKScene {
    let cameraNode = SKCameraNode()

    override func didMoveToView(view: SKView) {
        let center = CGPointMake(view.frame.size.width/2, view.frame.size.height/2)

        cameraNode.position = center
        addChild(cameraNode)
        camera = cameraNode
        let background = SKSpriteNode(color: SKColor.orangeColor(), size: view.frame.size)
        cameraNode.addChild(background)

        let sprite = SKSpriteNode(color: SKColor.redColor(), size: CGSizeMake(100, 100))
        sprite.position = center
        addChild(sprite)
    }

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        let move = SKAction.moveBy(CGVectorMake(200, 0), duration: 2)
        cameraNode.runAction(move)
    }
}
T. Benjamin Larsen
  • 6,373
  • 4
  • 22
  • 32
  • What do you mean by `connect the cameraNode not to the scene but rather to a separate "panning node"`? – damirstuhec Jun 25 '16 at 14:11
  • Never mind, it was a dead end, but the above should work just fine. – T. Benjamin Larsen Jun 25 '16 at 14:31
  • Your solution works. I have tried the same thing but my problem was that I was not adding my `cameraNode` as a child to the scene. I was convinced I don't have to explicitly do that, since there is already a `camera`  property provided by the system, waiting to be initialised and ready to go if needed. Strangely enough, the panning and everything else regarding camera worked as expected even though I was not adding it to the scene as a child. Now it works. Thank you! – damirstuhec Jun 25 '16 at 14:46