2

I have tried to call a method in a viewcontroller after a specified action in the GameScene class in spritekit but to no avail. I would greatly appreciate if anyone could post an example of calling a simple method from inside the viewcontroller class (that's linked to the skscene) in the skscene class.

I would greatly appreciate any answers!

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
Jonathan H.
  • 939
  • 1
  • 7
  • 21
  • check out my answer for this question: http://stackoverflow.com/questions/28184461/how-to-call-method-from-viewcontroller-in-gamescene/28187085#28187085 – hamobi Jan 30 '15 at 03:49
  • I have tried that but it would not let me call the method on the variable declared in skscene 'viewcontroller'. So it basically would not let me do 'viewcontroller.gameOver()' – Jonathan H. Jan 30 '15 at 03:59
  • 1
    Post some code because I'm having trouble understanding where the problem is – hamobi Jan 30 '15 at 04:00

1 Answers1

3

It's a question of referencing your viewController in your GameScene. There are many approaches, one is to use this in the GameScene:

let controller = self.view?.window?.rootViewController as GameViewController

Now this will return the viewController that is showing the scene at that moment. So if you have more then one controller, you should take care with this.

Tokuriku
  • 1,332
  • 13
  • 22
  • Can I attached a method name found in the root viewcontroller using dot syntax after the constant 'controller'? – Jonathan H. Jan 30 '15 at 04:05
  • Yes, you should have access to all the methods in the viewController as long as the trailing `as Something` references the particular Type of your game view controller. So if you didn't change that part, the code here should reference it nicely, I just tried it ;) – Tokuriku Jan 30 '15 at 04:09