从 SKScene 呈现 UIViewController 显示黑屏

Presenting a UIViewController from SKScene shows black screen

此代码旨在向用户呈现 UIViewController "menu",而不是用黑屏来迎接他们。

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

我正在从 SKScene 过渡。

要展示来自另一个 SKSceneSKScene 你应该这样做,例如:

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

您不需要取回您的 currentViewController,因为您始终可以访问 scene

view

如以下评论所述,有多种方法可以调用游戏中实现的功能 viewController,一种可能是创建 delegate/protocol 如代码所示:

GameScene 示例:

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 示例:

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)
    }
}

输出: