1
votes

I have quite a specific problem. I used to be presenting my Game Scene from a view controller with the default code. However I Added another SKScene and am now presenting my game scene from that using this code:

//In an SKScene (not Game scene)
let scene = GameScene()
let skView = self.view!
skView.ignoresSiblingOrder = true
scene.scaleMode = .aspectFit
let push = SKTransition.push(with: SKTransitionDirection.right, duration: 0.4)
skView.presentScene(scene, transition: push)

My problem is that my game scene seems to no longer recognize my SKS file as when I run the line of code:

//in gamescene
sprite = self.childNode(withName: "sprite") as! SKLabelNode

It doesn't find anything when unwrapping. When I present my SKScene from GameViewController using the code:

if let scene = GKScene(fileNamed: "GameScene") {

        // Get the SKScene from the loaded GKScene
        if let sceneNode = scene.rootNode as! GameScene? {

            // Copy gameplay related content over to the scene
            sceneNode.entities = scene.entities
            sceneNode.graphs = scene.graphs

            // Set the scale mode to scale to fit the window
            sceneNode.scaleMode = .aspectFit

            // Present the scene
            if let view = self.view as! SKView? {
                view.presentScene(sceneNode)

                view.ignoresSiblingOrder = true
            }
        }
    }

Everything works. What am I doing wrong? Thanks in advance.

1
More context is necessary to understand what is happening. It seems like you're writing this code within a UIViewController (is this correct?) If so. when you write self.childNode()... doesn't self refer to a UIViewController? Shouldn't it be scene.childNode()? And how do you add the child node in the first place?Acoop
@Acoop I updated my question. Thanks for the commentary, if you need anymore information please ask.joshLor
Thanks. How do you instantiate the sprite to begin with? And why do you create a second SKView within the first one, and then replace the view?Acoop
@Acoop I would be totally open to a new method of presenting my SKScene(that is sort of what I am looking for), also I am quite sure that the object is not the problem as if I present it from my view controller it works just fine.joshLor
Your scene transition seems fine. (I checked the docs.) However, I still think your issue is in the use of self.childNode(). I assume that this line is in the second scene? Is this right? And where was the sprite originally added? To the first scene?Acoop

1 Answers

1
votes

I think the reason that it crashes when you present from within the SKScene, and not from the UIViewController is because of 2 reasons.

  1. You are creating 2 different classes, that would probably have different code. When you present from the SKScene, you create an object of type GameScene but when you present from the UIViewController you create an object of type GKScene.
  2. When you create the object of type GameScene, you use an initializer that does not take into account the attached SKS File. But when you create the object of type GKScene you do.

Therefore, I would edit the first block of code to be:

//In an SKScene (not Game scene)
if let sceneContainer = GKScene(fileNamed: "GameScene") {
    let skView = self.view!
    skView.ignoresSiblingOrder = true
    //Something comparable to the following line. I don't have a project set up, but let the compiler run you through the exact syntax.
    let scene = sceneContainer.rootNode as! GameScene?
    scene?.scaleMode = .aspectFit
    let push = SKTransition.push(with: SKTransitionDirection.right, duration: 0.4)
    skView.presentScene(scene!, transition: push)
}

The doc for getting the SKScene from a GKScene can be found here.