JohnV
JohnV

Reputation: 285

iPhone Swift Changing your initial Scene

Now, I know how to transition scenes in SpriteKit games. The tricky thing I can't wrap my brain around is changing the initial one that is loaded. My game I have created jumps straight into the game. That initial scene is called GameScene.swift. I created a MainMenuScene.swift and I am trying to change it to load that first instead. Then once clicked I'll do an SKTransition to the next scene just as I have working later in the game.

This is my view Controller code:

override func viewDidLoad() {
    super.viewDidLoad()

    if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene {
        // Configure the view.
        let skView = self.view as SKView
        //skView.showsFPS = true
        //skView.showsNodeCount = true

        /* Sprite Kit applies additional optimizations to improve rendering performance */
        skView.ignoresSiblingOrder = true

        /* Set the scale mode to scale to fit the window */
        scene.scaleMode = .AspectFill

        skView.presentScene(scene)
    }
}

I thought changing the "GameScene" to "MainMenuScene" would do the trick but nothing gets loaded at all. Then I thought maybe it was the size but that all gets the same setting as the other Scene. Even trying to add the Node count, nothing shows up. So I am assuming nothing is being loaded at all. Maybe I am blind and stupid but I just can't see it.

Thanks in advance.

Upvotes: 1

Views: 1243

Answers (2)

JGrn84
JGrn84

Reputation: 750

Make your viewController like this:

class GameViewController: UIViewController {

           override func viewDidLoad() {
                super.viewDidLoad()
                let scene = MainMenuScene(size: view.bounds.size) //This can be any scene you want the game to load first
                let skView = self.view as! SKView
                skView.showsFPS = false
                skView.showsNodeCount = false
                skView.ignoresSiblingOrder = true
                scene.scaleMode = .ResizeFill
                skView.presentScene(scene)
        }

    }

In your MainMenuScene.swift (and every other scene you want included in your game) make sure that the class itself starts like this:

class chooseScene: SKScene {

override func didMoveToView(view: SKView) {

} }

This is the code I have used in my viewController and SKScenes and I had the exact same problem as you a few months ago.

Upvotes: 4

hamobi
hamobi

Reputation: 8130

in your viewcontroller try this

// Configure the view.
let skView = self.view as! SKView
skView.showsFPS = true
skView.showsNodeCount = true

/* Sprite Kit applies additional optimizations to improve rendering performance */
skView.ignoresSiblingOrder = true

let scene = MainMenuScene(size: skView.frame.size)

/* Set the scale mode to scale to fit the window */
scene.scaleMode = .AspectFill

skView.presentScene(scene)

in MainMenuScene, make sure you add these initializers

override init(size: CGSize) {
    super.init(size: size)
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

Upvotes: 0

Related Questions