SKCameraNode and Xcode Debug View Hierarchy
As of Xcode v9.3.1, Debug View Hierarchy displays SpriteKit scenes with a SKCameraNode incorrectly. My observation and workaround.
As I'm just starting my SpriteKit journey, I do stumble upon quite a few problems along the way. The most common cause are of course my lack of experience with both Swift and Apple's frameworks, so finding solutions helps me learn and evolve.
Now and then I encounter things that might not necessary be because of me, like I did the other day when I was getting into Xcode's Debug View Hierarchy
. I did run into some peculiar behavior when it comes to SpriteKit
, SKCameraNode
and the debugger in the current version of Xcode, v9.3.1.
SKScene without SKCameraNode
I've setup a very basic SpriteKit project to clearly display the behavior. Here we have a scene with just 8 sprites in the SpriteKit editor. Running this app gives us the exact same view as we had in the editor, as we would expect.
Let's enter the view debugger while running the app, and voilà, we get the exact same view in the debugger too, just as we had in the app and editor.
SKScene with SKCameraNode
So far so good, now let's add a SKCameraNode
to the scene and do the same test again.
Having added the SKCameraNode and running the app one more time, we still get the expected result. The camera sees the scene as we have defined it, and the view in the app is correct.
So let's enter Debug View Hierarchy
once again...
Woah!
This not what I'd expect. The view when running the app is correct, the same as in the editor, but the view I get in the debugger is not the same. The view seems to be offset by half a screen. This happens consistently as soon as I add a camera to a scene, including to the default SpriteKit scene when starting a new project.
It makes no difference if adding the camera via the SpriteKit editor or if adding the camera programmatically in code.
The Anchor Point Workaround
I've found by changing the anchor point for the scene from the default value of (0.5, 0.5)
to (0, 0)
fixes the problem, and then debug view works as expected with SKCameraNode
.
While changing the anchor point makes the debugger display the view as expected when using a camera, to not be able to use debug view with SpriteKit and SKCameraNode
unless the scene has an anchor point of (0, 0) does not seem correct to me, or to be the intended behavior.
I'd expect the debugger to display the same view as I see when running the app, no matter what anchor point I am using for the scene.
Is this a bug in Xcode v9.3.1? Or am I missing something?