Presenting a UIViewController from SKScene shows b

2019-02-27 19:09发布

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条回答
甜甜的少女心
2楼-- · 2019-02-27 19:27

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

查看更多
登录 后发表回答