Chapter 2: Manual Movement :: Page 60 Rendered Node Count

In the Note:

Besides the FPS, SpriteKit also displays the count of nodes that it rendered in the last pass.

I found that the node count showed +1 more than the SKSpriteNode children I added:

let background = SKSpriteNode(imageNamed: "background1")
let zombie = SKSpriteNode(imageNamed: "zombie1")
addChild(background) // node 1
addChild(zombie)     // node 2

I reread Chapter 1: Sprites :: Page 54 Nodes and z-position and found this fun fact.

Every node has a property you can set named zPosition, which defaults to 0.

I implemented a free function outside of GameScene to test the SKScene type.

func isNode(test gameScene: SKScene) {
  print(#function + " zPosition: \(gameScene.zPosition)")
  gameScene.zPosition = -2 // try to set before background
  print(#function + " zPosition -2: \(gameScene.zPosition)")
// And called it within didMove(to:)
isNode(test: self)

GameScene’s zPosition was 0.0 so it must be the +1 node.

…But I when I tried to modify that value I got this in the debugger:
SKScene: Setting the zPosition of a SKScene has no effect.

This got me thinking:
I’m assuming more than one SKScene node may exist per app.
If the background zPosition is -1, and the GameScene (it’s parent) is always 0.0…
Question 1: Is GameScene’s zPosition 0.0 only compared to other SKScenes (parent nodes)?
Question 2: Or is GameScene only given a zPosition property because it is a node?


I believe you can have only one SKScene per SKView. SKScene is the thing that gets run by the animation loop; it in turn renders all the nodes you have added to it. You can switch to different views, or present a different scene in the view, but a scene won’t have a position relative to other SKScenes.

I think you are right, a scene has a zPosition because it is an SKNode. It is 0 because something has to be nailed down. Making the background -1 means any node added with the default zPosition of 0 will be visible.

Nice to see you back. :slight_smile:

1 Like

Hi Steve! :wave: :smile:

Thanks for explaining it to me, so new to it all but the difficulty is perfect! Thanks for suggesting it, I’m REALLY enjoying this book so far - sad to hear it’s not being supported anymore.

Glad to be back, I got sick, got a Nintendo Switch for my birthday still sick - got lost in Zelda for a solid 10 days (that game is brilliant) and then got sick again about 20 days later… Sad times man.

FYI, I’d been meaning to respond to post of yours in the iOS 11 by Tutorials forum about the Headlines app - your suggestions worked well :+1: and I have some url paths to share that will work with their new v2.

1 Like

This topic was automatically closed after 166 days. New replies are no longer allowed.