Presenting a UIViewController from SKScene shows black screen

前端 未结 1 1425
慢半拍i
慢半拍i 2021-01-24 03:06

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

let currentViewController:UIVi         


        
相关标签:
1条回答
  • 2021-01-24 03:34

    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:

    0 讨论(0)
提交回复
热议问题