Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use storyboards with spriteKit using swift

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 meant to use storyboards with spriteKit. I find it hard to design a nice interface for a game's menu without a good visual builder. Is there a way to start a spriteKit application using storyboards, then with a click of a "start game" button, switch to spriteKit scenes, then when you lose the game, in the code switch back to storyboards(using swift)? Please help and thanks.

-Callum-

like image 770
Callum Ferguson Avatar asked Mar 11 '15 02:03

Callum Ferguson


People also ask

How do I add a storyboard in Swift?

From the File menu, choose New→New File... In the New File dialog, make sure you have selected the Resource subcategory of the iOS category on the left. Then choose the Storyboard item on the right and press Next (see Figure 4-24). In this screen, pick the Device Family for which you want to create your storyboard.

Can you use SpriteKit with SwiftUI?

SwiftUI's SpriteView lets us render any SKScene subclass right inside SwiftUI, and it will even resize the scene if you request it. The game scene you create is fully interactive, so it works just like a regular SKView would do in UIKit.

How do I Storyboard in Xcode?

To get started with Xcode Storyboard, just create a new project (empty Single-View Application). Once Xcode is up and running, click . storyboard file, open and create UI for your application.


1 Answers

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.

like image 92
ZeMoon Avatar answered Nov 15 '22 07:11

ZeMoon