0

How can I switch to a UIViewController while an SKScene is loaded and vice versa? I have so far been able to implement NSNotificationCenter to tell my GameViewController to switch, but I am having trouble with what code to run to actually change Scenes. Here is my GameViewController.swift file:

import UIKit
import SpriteKit
import StoreKit

class GameViewController: UIViewController {
    override func viewDidLoad() {

     NSNotificationCenter.defaultCenter().addObserver(self, selector: "openArtboard:", name:"OpenArtboard", object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "openHomeScreen:", name:"OpenHomeScreen", object: nil)

    //Load the initial home screen
    let scene = homeScreen(size: view.bounds.size)
    let skView = view as SKView
    skView.showsFPS = true
    skView.showsNodeCount = true
    skView.ignoresSiblingOrder = false
    scene.scaleMode = .ResizeFill        
    skView.presentScene(homeScreen(size: skView.bounds.size))
}

    func openArtboard(NSNotificationCenter) {

    //Dont know what to put here

}

    func openHomeScreen(NSNotificationCenter) {
    //Dont know what to put here
}
}

Any help is much appreciated.

jwade502
  • 898
  • 1
  • 9
  • 22
  • 1
    What do you mean switch to a view controller from an SKScene? The scene is displayed in a view controller... – Kendel May 16 '15 at 21:47
  • In my game I have to switch to a regular UIViewController to display another part of the app that is not made in SpriteKit. I used the same code like hamecanecha said below, but when I try to switch back from the UIViewController to an SKScene, I get a memory leak. – jwade502 May 17 '15 at 23:39

1 Answers1

-1

Check out: How to dismiss SKScene?

I don't have the time to test these answers but try them out.

Remember, the UIViewController contains the SKView, so by changing the current view controller, you'll remove the current SKView too. Try just going to another SKView using a segue.

And how to load the scene FROM the viewcontroller? It's already there in your code:

let scene = homeScreen(size: view.bounds.size)
let skView = view as SKView
skView.showsFPS = true
skView.showsNodeCount = true
skView.ignoresSiblingOrder = false
scene.scaleMode = .ResizeFill        
skView.presentScene(homeScreen(size: skView.bounds.size))
Community
  • 1
  • 1
Jake Crastly
  • 462
  • 3
  • 9
  • I tried using this same exact code but there is a memory leak when you switch like this. The UIViewController I am presenting from stays in memory I assume. I tried calling self.artboardViewController.dismissViewControllerAnimated(true, completion: nil) but the memory is still high. – jwade502 May 17 '15 at 23:37
  • Have you tried everything in the question I showed you? There are many answers including loading a nil scene etc. Are you sure they all have memory leaks? And is there really a memory leak in instruments? – Jake Crastly May 18 '15 at 00:45
  • Yep...it seems that my SKScene is being dismissed properly, it's the UIViewController that is staying in memory when I try to switch back to an SKScene. :/ And there is a memory leak of about ~70mb increase each time I try to do it, which is strange because the artboard ViewController only has one button right now. – jwade502 May 18 '15 at 04:50
  • Well there should always be at least one UIViewController at the top of the stack taking memory, but you're telling me EACH and EVERY time you present a scene you lose another 70 mb memory? – Jake Crastly May 18 '15 at 05:23
  • Yes it sure does :( I wonder if it is because a new skview is being created every time I call the openHomeScreen function? – jwade502 May 18 '15 at 06:01
  • Have you tried not just setting the scene to nil, but also removing the skview? Cause that's all I can think of – Jake Crastly May 18 '15 at 07:48