0

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.

Osaama Shehzad
  • 147
  • 1
  • 2
  • 12

1 Answers1

0

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!

cjrieck
  • 974
  • 6
  • 11
  • thank you so much! can you please provide some example in terms of code to achieve that? – Osaama Shehzad Jul 19 '16 at 08:43
  • @cjrek thank you so much for this! one thing i am really confused about is that where does my code for sprite project goes in? is it supposed to go where you commented in your code, or somewhere else ? i am knew to Swift which is why i am troubling understanding the chain of code – Osaama Shehzad Jul 20 '16 at 08:17
  • Hey @OsaamaShehzad! You can mix SpriteKit APIs in with UIKit no problem, so you can put your scene setup code right where my comment is in the example. Hope this helps! – cjrieck Jul 20 '16 at 17:08