从 SKScene 呈现 UIViewController 显示黑屏 [英] Presenting a UIViewController from SKScene shows black screen

查看:30
本文介绍了从 SKScene 呈现 UIViewController 显示黑屏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码旨在向用户显示 UIViewController菜单",而是以黑屏迎接他们.

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)

我正在从 SKScene 过渡.

推荐答案

要从另一个 SKScene 中呈现一个 SKScene 你应该这样做,例如:

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

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

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

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

输出:

这篇关于从 SKScene 呈现 UIViewController 显示黑屏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆