0

I am developing a game with five scenes (SKSecne) in swift. I am using the following function to show the score in the Game Center at the end of each scene. Currently I have to copy the function to all the scene files. How can I modify the function so I can call it from all the scene files without duplicating it?

func showLeader() {
    let viewControler = self.view?.window?.rootViewController
    let gameCenter = GKGameCenterViewController()
    gameCenter.gameCenterDelegate = self
    viewControler?.presentViewController(gameCenter, animated: true, completion: nil) }
Julien
  • 1

1 Answers1

0

One solution is just create a subclass of SKScene and use it like parent for others five scenes.

class BasicScene: SKScene {
   func showLeader() {}
}

class Scene1: BasicScene {
   // call showLeader() when needed 
}
Arsen
  • 10,815
  • 2
  • 34
  • 46
  • thanks that worked! I had problem with functions that work with `self` . I wasn’t able to call them from different scenes. Now I can use subclass to fix this issue. – Julien Jun 26 '16 at 03:18