How do I use storyboards with spriteKit using swift

后端 未结 1 1989
误落风尘
误落风尘 2021-01-01 03:43

One of my main reason for using Xcode instead of other applications for making iOS apps was the storyboard interface builder. I was unhappy when I found out that you are not

相关标签:
1条回答
  • 2021-01-01 04:15

    A SpriteKit Scene is presented on an instance of a SKView, which is a subclassed UIView.

    For an iOS game made using SpriteKit, one needs to have at least one viewController set up, either programatically in the App delegate or in a storyboard, on which the SKScene can be displayed. It is on the main view of this VC that a SKScene will be presented in.

    So if you are using a storyboard, the iOS game will have to instantiate the root viewController from it. You can easily design your UI on the viewController, and present the game from the code on the press of a button, either in the same viewController or a new one. All this will be evident once you read a beginner tutorial for SpriteKit using Swift like this.

    Let's say your root viewController has your main menu (on another view called menuView), with a play button on it. Now presenting a game on the press of a button would look something like this:

    class MyViewController: UIViewController {
      @IBOutlet wear var menuView: UIView?
      @IBOutlet weak var playButton: UIButton?
    
      @IBAction func likedThis(sender: UIButton) {
        //Hide the menu view
        menuView.hidden = true
    
        //instantiate and present the scene on the main view
        let scene = MyScene(size: view.bounds.size)
        let skView = self.view as SKView
        skView.presentScene(scene)
      }
    }
    

    As for going back to the main menu from the scene, have a look at this answer.

    0 讨论(0)
提交回复
热议问题