0

I have one viewController called NeighborhoodViewController. It loads a scene into the viewController, called NeighborhoodScene.sks. This works absolutely fine. I have rigged up a segue from a specific node in the scene through NeighborhoodViewController. These two functions control this:

func createSegue(title: String) {
    performSegueWithIdentifier(title, sender: self)
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if let viewController: SearchViewController = segue.destinationViewController as? SearchViewController {
        viewController.sceneName = "game_1_1"
    }
}

This all works fine. The segue is being called. The segue is meant to bring up another VC called SearchViewController, which is supposed to bring up its own scene, SearchScene1.sks.

This is the code for that:

import UIKit
import SpriteKit

class SearchViewController: UIViewController {
var sceneName: String?
var chosenScene: GameScene?

override func viewDidLoad() {
    super.viewDidLoad()

    if sceneName == "game_1_1" {
        chosenScene = GameScene(fileNamed: "SearchScene1")
    } // TODO: add the rest of the presented scenes here

    if let scene = chosenScene {
        // Configure the view.
        let skView = self.view as! SKView
        skView.showsPhysics = true
        skView.showsFPS = true
        skView.showsNodeCount = true

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

        /* Set the scale mode to scale to fit the window */
        scene.scaleMode = .AspectFill
        scene.viewController = self
        skView.presentScene(chosenScene)
    }
}
}

If I print out the restorationidentifier for skView, it pertains to the view within the SearchViewController, which is perfect. It all seems to be working fine until skView.presentScene(chosenScene) at which point it randomly seems to be setting up NeighborhoodViewController? How do I make it call up SearchViewController and the associated sks scene -- SearchScene1?

  • Any particular reason your approach is this complicated using multiple view controllers? Usually in a sprite kit game you just use 1 GameViewController that loads your first scene. You than transition directly from 1 scene to another within the scenes. – crashoverride777 Sep 14 '16 at 13:38
  • The reason is that I'm basically building six or seven completely separate games that have no real resemblance to each-other. Having a view controller for each one seemed like a good plan as then I can handle each view differently... I also wanted to reuse some of the gamescene.swift data for different scenes if I wanted to use the same game mechanics in different environments. –  Sep 14 '16 at 13:41

1 Answers1

1

The segue is used for bring the nextviewcontroller first.