如何使用 SpriteKit 在 Xcode13 中以编程方式设置根视图控制器?
How to set root view controller programmatically in Xcode 13 with SpriteKit?
在Xcode12的SpriteKit起始模板中,没有SceneDelegate
文件,所以UIKit应用程序指令不工作。删除 Main.storyboard
和 info.plist
条目后,出现黑屏。这是 Xcode 12 中 SpriteKit 模板的 AppDelegate
:
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
return true
}
}
据我了解,我们需要在此处集成 GameScene,而不是 SceneDelegate,但是尝试像 guard let winScene = (scene as? SKScene) else { return }
那样对其进行类型转换是行不通的,因为 application
函数是不同的。
如何进行?
谢谢!
如果要删除 Main.Storyboard,您需要将以下内容添加到您的 AppDelegate
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
let viewController = GameViewController()
window?.rootViewController = viewController
window?.makeKeyAndVisible()
return true
}
并且在GameViewController.swift中您需要添加以下内容:
override func loadView() {
self.view = SKView(frame: UIScreen.main.bounds)
}
在Xcode12的SpriteKit起始模板中,没有SceneDelegate
文件,所以UIKit应用程序指令不工作。删除 Main.storyboard
和 info.plist
条目后,出现黑屏。这是 Xcode 12 中 SpriteKit 模板的 AppDelegate
:
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
return true
}
}
据我了解,我们需要在此处集成 GameScene,而不是 SceneDelegate,但是尝试像 guard let winScene = (scene as? SKScene) else { return }
那样对其进行类型转换是行不通的,因为 application
函数是不同的。
如何进行? 谢谢!
如果要删除 Main.Storyboard,您需要将以下内容添加到您的 AppDelegate
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
let viewController = GameViewController()
window?.rootViewController = viewController
window?.makeKeyAndVisible()
return true
}
并且在GameViewController.swift中您需要添加以下内容:
override func loadView() {
self.view = SKView(frame: UIScreen.main.bounds)
}