How to use storyboards with spriteKit with swift

One of my main reasons for using Xcode instead of other apps to create iOS apps is the storyboard interface builder. I was unhappy when I found out that you are not going to use storyboards with spriteKit. It's hard for me to create a nice interface for the game menu without a good visual designer. Is there a way to launch the spriteKit application using the storyboard, then with one click on the “start the game” button, switch to spriteKit scripts, and then when you lose the game, return to the storyboard in your code (using the fast one)? Please help and thanks.

-Callum -

+5
source share
1 answer

The SpriteKit scene is presented on an instance of SKView, which is a subclass of UIView.

For an iOS game created using SpriteKit, you must configure at least one viewController, either programmatically in the application delegate or in a storyboard on which SKScene can be displayed. On the main screen of this VC, SKScene will be presented.

So, if you are using a storyboard, an iOS game will have to instantiate the root viewController. You can easily create your user interface on the viewController and present the game from the code by pressing a button either in the same viewController mode or in a new one. All of this will be apparent after you read SpriteKit's primer using Swift, like this .

Say your root viewController has your main menu (in another view called menuView), with a play button on it. Now the representation of the game by pressing a button will 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 returning to the main menu from the scene, look at this answer .

+6
source

Source: https://habr.com/ru/post/1215103/


All Articles