1

Below I have two and a half different ways to set up my game using view controllers and SKScene. The half I haven’t put so much time into yet as I’m hoping one of these other solutions will work. described in more detail below are:

  1. MainViewController -> GameViewController(holds the SKScene)
  2. GameViewController(holds the SKScene) - Using Protocols

0.5 Two SKScenes/Two swift custom classes.

Number 1

MainViewController -> GameViewController(holds the SKScene)

when the game is over in the SKScene - I want to dismiss GameViewController and return to MainViewController.

I’ve had no luck in getting that to work. I’ve tried dismissing the view controller and the closest I’ve come is dismissing the SKScene using something like(in SKScene:

self.view?presentScene(nil)

or

self.dismiss(animated: false, completion: nil)
 self.presentingViewController?.dismiss(animated: false, completion: nil)

but I’m unable to go back to the MainViewController. I’ve tried creating a reference to the GameViewController to pop it like:

weak var gameViewController:GameViewController? = GameViewController()

Unfortunatly, I’m unable to find the right functions to dismiss it properly.

After a lot of searching without finding an answer I thought I would try a different method.

Number 2

GameViewController(holds the SKView) then have the GVC hold the buttons and labels that I wanted and show the scene on top. this was a fail as The buttons and labels don’t have a zPositions and appeared on top of the scene.

I tried doing .isHidden = true on them before calling the scene and that seemed to work but when I dismiss the scene and attempt to change them back to .isHidden = false (tried using protocols) I was unable to get it to work. (was going off of what stvn describes How to reference the current view controller from a sprite kit scene)

I suspect this second way could be a good way of doing it but maybe I’m just not approaching it correctly.

Number 0.5

then finally what I’m thinking about now is just making the second scene to “flip” back and forth between. I believe this is the recommended way of doing it but I’m not really sure how to approach it. If I did this version. I would like to have a gameScene.swift/gameScene.sks for each view (perhaps using something like presenting a uiviewcontroller for skscene.

I would prefer the first way with having two view controllers as I think that the layout for my game would be much easier to do using a few view controllers.
My least preferred method is multiple scenes as I’m just not that great with sprite kit and math for laying out everything.

Is the first method even possible? If so any suggestions would be great. If not what would be the recommended way of doing this?

jamesC
  • 422
  • 6
  • 25

1 Answers1

0

In method 1 if your MainViewController is a UINavigationController you should have no issues pushing and popping whatever other ViewControllers you need to.

e.g. in GameViewController:

self.navigationController!.popViewController(animated: true)
dijipiji
  • 3,063
  • 1
  • 26
  • 21