SKScene in UIViewController

2019-09-10 21:58发布

问题:

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!