ios - UIViewController 中的 SKScene

标签 ios swift uiviewcontroller skscene

我正在制作具有多种功能的应用程序,其中之一应该是游戏。普通的碰撞游戏,你击中物体并得分。但我的应用程序是基于单一的应用程序。

当我创建一个新的 swift 文件时,如何将 SKScene 添加到 UIViewController?

任何帮助将不胜感激。

最佳答案

SKScene 需要添加到 SKView,它是 UIView 的子类。创建 View Controller 时,您可以将 view 属性设置为 SKView 或添加 SKView 作为 subview ,然后添加您的通过 SKView 上的 presentScene: 方法将 SKScene 发送到该 SKView。以下是如何实现这一目标的示例:

import SpriteKit

class SomeViewController: UIViewController {
    func viewDidLoad() {
        super.viewDidLoad()

        let sceneView = SKView(frame: view.frame)
        let scene = SKScene()
        // Do any other scene setup here
        view.addSubview(sceneView)
        sceneView.presentScene(scene)
    }
}

如果有小语法错误,我们深表歉意。没有机会对此进行测试,而且我对 SpriteKit API 的内存很模糊。希望这有帮助!

关于ios - UIViewController 中的 SKScene,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38439734/

相关文章:

iOS : Handle multiple uilocalnotification with multiple same viewcontroller

ios - Xcode LLVM 3.1 和 GCC_OPTIMIZATION_LEVEL

ios - 如何解析嵌套的 JSON。我得到 nil 值作为输出

iOS:AVSpeechSynthesizer:需要在耳机左声道中朗读文本

Swift:从有符号 2 的补码 Uint8 到十进制数

ios - 如何从 SKScene 到 UIViewController?

ios - 按下 “Edit” UITableViewRowAction 时无法切换到另一个 UIViewController

ios - 步骤:Add the Push Notifications feature to your App ID报错

ios - ViewController 中的 Swift 错误 "Use of undeclared type"

objective-c - 以编程方式创建 UIView 和 UIViewController 并链接在一起的正确方法