How to create a Main Menu?

Hi guys,
I am pretty on a good way to “finish” the game logic on my game. I started on the Game Scene and put all my logic in there as learned in the book. I still got some tweaks to do, but I am pretty hapy with my process so far.

Still I think of a Main Menu as an entry screen and to add an option starting the game, resuming the game and accessing the settings. Further a Game Level Screen should be available to the user, when he selects to resume the game.
Unfortunately I do not have a clue how to proceed from my current state of the game to add another Game Scene with a transition.
For the levels of my game I proceeded as described in the Cat Nap Game, but I do not know how this might help me transitioning Scenes and adding a Main Menu.

Does anyone of you guys know how to help me? Any suggestions? Any recommended reading / tutorial material?

Would be so happy for support - please do not leave me out in the dark…

Thanks in advance,
Johannes

I would setup the main menu in a scene editor file. It’s much easier than programmatically arranging the menu items.

Then in GameViewController.swift, present your scene with that editor file like this: GameScene(fileNamed:"MainMenuScene"). This will be the first thing that shows when the app is opened.

In touchesBegan in GameScene.swift you can use the touchedNode.name to add actions for whatever menu items the player selects. For instance, with the “play” button you can present the scene again, but without the main menu editor file to begin the game.

That’s how I’ve been doing it, hope that helps!

1 Like

Hi Ben,
and thanks for you help fist of all.

I tried what you mentioned but I ran into some errors :frowning:

First of all I created 2 new files.

  1. MainMenu.swift
  2. MainMenu.sks

Then I replaced in my GameViewController.swift the line
if let scene = GameScene.level(1) {
with
if let scene = GameScene(fileNamed: "MainMenu") {
and tried to build and run the app, but nothing worked for me and the app crashed, since there was now an error that popped up in my GameScene.swift file?!? This somehow confused me, because I did not change anything in GameScene.swift although I might should have to - but I do not know what and how. :frowning:

In my Storyboard I have only one ViewController layed out which is the GameViewController. Should there be also another ViewController?

I really got stuck here :thinking:
Any additional clue?

What is the MainMenu.swift file being used for?

I’m not 100% sure on this, but GameScene(fileNamed: "MainMenu") may be getting the MainMenu.swift file instead of the MainMenu.sks file. I would try removing or renaming MainMenu.swift.

My guess is:

MainMene.swift is the class the manages MainMenu.sks

It might help to assign the “MainMenu” class in the MainMenus.sks file in the “Attributes inspector”.

To implement an initial scene from the primary view controller (GameViewController) try this
if let view = self.view as! SKView? {
// Load the SKScene from ‘MainMenu.sks’
if let scene = SKScene(fileNamed: “MainMenu”) {

// Present the scene
view.presentScene(scene)
}

1 Like

Thanks Zoneman,
assigning the MainMenu class in the Attributes Inspector did the job — YAY! :slight_smile:

Thanks you guys

1 Like

there is a main menu section on or around chapter 4 that should help.

1 Like