1

This code, intending to present the user with the UIViewController "menu", instead greets them with a black screen.

let currentViewController:UIViewController=UIApplication.shared.keyWindow!.rootViewController!
let mymenu = menu()
currentViewController.present(mymenu, animated: true, completion: nil)

I'm transitioning from a SKScene.

Alessandro Ornano
  • 34,887
  • 11
  • 106
  • 133

1 Answers1

1

To present a SKScene from another SKScene you should do for example :

let nextScene = MainScene(size: self.scene!.size)
self.scene?.view?.presentScene(nextScene, transition: SKTransition.doorway(withDuration: 1))

You don't need to retrieve your currentViewController because you have always access to the view of your scene


As explained to the comments below, there are various methods to call a function implemented to your game viewController, one could be to create a delegate/protocol as showed in this code:

GameScene example:

import SpriteKit
protocol GameViewControllerDelegate: class {
    func callMethod(inputProperty:String)
}
class GameScene: SKScene {
    weak var gameViewControllerDelegate:GameViewControllerDelegate?
    override func didMove(to view: SKView) {
        gameViewControllerDelegate?.callMethod(inputProperty: "call game view controller method")
    }
}

GameViewController example:

class GameViewController: UIViewController, GameViewControllerDelegate {
    override func viewDidLoad() {
        super.viewDidLoad()
        if let view = self.view as! SKView? {
            // Load the SKScene from 'GameScene.sks'
            if let scene = SKScene(fileNamed: "GameScene") {
                let gameScene = scene as! GameScene
                gameScene.gameViewControllerDelegate = self
                gameScene.scaleMode = .aspectFill
                view.presentScene(gameScene)
            }
            view.ignoresSiblingOrder = true
            view.showsFPS = true
            view.showsNodeCount = true
        }
    }
    func callMethod(inputProperty:String) {
        print("inputProperty is: ",inputProperty)
    }
}

Output:

enter image description here

Alessandro Ornano
  • 34,887
  • 11
  • 106
  • 133
  • But I'm transitioning to a view, not a SKScene. –  Jan 27 '17 at 22:57
  • You should distinguish `UIKit` elements from `Sprite-kit` elements, especially if you work with a game. You also don't have a view but a `SKView` that contain `SKScene`'s so you can't "transitioning" to a view – Alessandro Ornano Jan 27 '17 at 23:02
  • I'm sorry, I don't get it. I want to call a function in my SKScene, that will present the player with a uiviewcontroller. –  Jan 27 '17 at 23:13
  • Ah right, that's another story :-) ,anyway you can present the player view or any other `UIKit` view on your scene `self.scene?.view?.addSubview()` – Alessandro Ornano Jan 27 '17 at 23:17
  • Okay, sorry, thanks, but how DO I present the player with a uiviewcontroller? –  Jan 27 '17 at 23:21
  • You can't directly from a `SKScene`, it's the `GameViewController` that call another `UIViewController`. You can do it from a `SKScene` by create a delegate/protocol and , to your scene, you can call something like `self.delegate transitionToOtherViewController` (method inside your `GameViewController` that could make something like navigation pushViewController..) – Alessandro Ornano Jan 27 '17 at 23:31
  • I didn't. I put everything shown in your GameScene example in my GameScene. –  Jan 28 '17 at 09:36
  • Usually I test all my code, projects.You can download it from [here](http://www98.zippyshare.com/v/7iSgOAui/file.html) – Alessandro Ornano Jan 28 '17 at 09:40
  • You can implement some code inside that method to do it – Alessandro Ornano Jan 28 '17 at 09:41