4

I have been trying to learn how to use delegates recently and actually seem to understand it for the most part. However I have one problem.

I have 1 GameViewController, 1 StartScene and 1 MenuScene

In my GameViewController I have 2 methods that I would like to get called from MenuScene. So far I have been using NSNotification, which works fine but I am trying to use delegates.

So I have set up a protocol in MenuScene

 protocol MenuSceneDelegate {
 func gameOver()
 func showGameCenter()
}

I than referenced it like so

 var menuSceneDelegate: MenuSceneDelegate?

In GameViewController I added the MenuSceneDelegate at the top. All is very standard so far. However the main problem I have is that when I set the delegate in GameViewController like so

 let skView = view as! SKView!
    var scene = StartScene(size: skView.bounds.size)

    skView.ignoresSiblingOrder = true
    scene.scaleMode = .ResizeFill

    skView.presentScene(scene)

    scene.menuSceneDelegate = self   //Sets delegate to StartScene not MenuScene

it only applies to the StartScene. How can I set the GameViewController delegate from StartScene to MenuScene. Everything works fine if I would first present the MenuScene. However I present StartScene first and therefore I am trying to understand how I set the delegate to MenuScene.

I tried something like this below but it doesnt work and just doesnt seem right

 var menuScene = MenuScene(size: skView.bounds.size)
 menuScene.menuSceneDelegate = self

I would appreciate any help or support. Thank you very much

crashoverride777
  • 10,581
  • 2
  • 32
  • 56

1 Answers1

1

Basically the solution is to set the delegate when I change from start scene to game scene, not in the beginning.

crashoverride777
  • 10,581
  • 2
  • 32
  • 56
  • What do you mean by this? – Confused Dec 12 '16 at 10:47
  • That's a good question lol. This is such a old answer. I will update it later – crashoverride777 Dec 12 '16 at 11:16
  • Sorry to bother you. I have been climbing up and down the delegate/protocol and inheritance ladders for a couple of days. 2 steps up, 3 steps back, every time. – Confused Dec 12 '16 at 11:33
  • Hehe. It's not bad once you get the hang of it. I will update later – crashoverride777 Dec 12 '16 at 11:35
  • In regards to inheritance/subclassing check out this answer. Not sure its its helpful. I asked this last year http://stackoverflow.com/questions/30486232/swift-multiple-level-scenes – crashoverride777 Dec 12 '16 at 11:40
  • I'm yet to really figure out the difference between super.init and self.init and their various needs. I just keep trying variations until one works. – Confused Dec 12 '16 at 12:00
  • Super.init is calling the superclass. In my example BaseScene – crashoverride777 Dec 12 '16 at 12:07
  • yeah, I kind of grasp the basics... but the mapping of initialisation in my head is a bit like a GPS during an electric storm. – Confused Dec 12 '16 at 12:10
  • I recommend you have a look at the basic Swift documentation from apple, they are very good. I did not do this when I started coding and it caused me alot of lost time. https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Initialization.html – crashoverride777 Dec 12 '16 at 12:37