Presenting a UIViewController from SKScene shows black screen

时光怂恿深爱的人放手 提交于 2019-12-02 07:51:04

问题


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.


回答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:



来源:https://stackoverflow.com/questions/41890681/presenting-a-uiviewcontroller-from-skscene-shows-black-screen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!