本文介绍了UIViewController 中的 SKScene的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am making this app with multiple features, one of which is supposed to be a game. the normal collision game where you hit objects and score. but my app is a Single based application.

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

when i create a new swift file, how can i add a SKScene to a UIViewController?

任何帮助将不胜感激.

推荐答案

SKScene 需要添加到 SKView ,它是 UIView.创建视图控制器时,您可以将 view 属性设置为 SKView 或添加一个 SKView 作为子视图,然后添加您的SKScene 通过 SKView 上的 presentScene: 方法到那个 SKView.下面是一个关于如何实现这一目标的示例:

SKScene needs to be added to an SKView which is a subclass of UIView. When you create the view controller, you can either set the view property to be an SKView or add an SKView as a subview, then add your SKScene to that SKView via the presentScene: method on SKView. Here's an example on how you could achieve that:

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 的记忆很模糊.希望这会有所帮助!

Sorry if there are small syntactical errors. Didn't have a chance to test this and my memory of the SpriteKit API is hazy. Hope this helps!

这篇关于UIViewController 中的 SKScene的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 20:41