SKScene in UIViewController

倾然丶 夕夏残阳落幕 提交于 2019-12-12 03:44:27

问题


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.

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

any help will be appreciated.


回答1:


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

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!



来源:https://stackoverflow.com/questions/38439734/skscene-in-uiviewcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!