ios8 - Swift SpriteKit Game Center -


i have been trying split game center code helper class per numerous tutorials on website , others.

this have in gameviewcontroller.swift

func loadgamecenter() {      var localplayer = gklocalplayer.localplayer()      localplayer.authenticatehandler = {(viewcontroller, error) -> void in          if (viewcontroller != nil) {          self.presentviewcontroller(viewcontroller,          animated: true, completion: nil) //point 1         }         else {             println((gklocalplayer.localplayer().authenticated))         }     } } 

in menuscene.swift, skscene, have code.

func savehighscore(highscore:int) {      if gklocalplayer.localplayer().authenticated {         var scorereporter = gkscore(leaderboardidentifier: leaderboardid)         scorereporter.value = int64(highscore)          var scorearray: [gkscore] = [scorereporter]         gkscore.reportscores(scorearray, withcompletionhandler: {(error : nserror!) -> void in             if error != nil {                 println("error")             }         })     } } func showgamecenter() {      var vc = self.view!.window!.rootviewcontroller! //point 2     var gc = gkgamecenterviewcontroller()     gc.gamecenterdelegate = self     gc.viewstate = gkgamecenterviewcontrollerstate.leaderboards     vc.presentviewcontroller(gc, animated: true, completion: nil) } func gamecenterviewcontrollerdidfinish(gamecenterviewcontroller: gkgamecenterviewcontroller!) {      gamecenterviewcontroller.dismissviewcontrolleranimated(true, completion: nil) } 

now have been trying put above mentioned code helper class called gamecenter.swift, subclass of nsobject. wrote after class name make methods mentioned above accessible in other scenes (as per tutorials).

class var sharedinstance: gamecenter {     struct static {         static let instance = gamecenter()     }     return static.instance }  class func loadgamecenter() {     gamecenter.sharedinstance.loadgamecenter() }  class func savehighscore() {     gamecenter.sharedinstance.savehighscore(highscore) }  class func showleaderboard() {     gamecenter.sharedinstance.showleaderboard() } 

the problem having when trying call class functions menuscene.swift, example "gamecenter.loadgamecenter()" or "gamecenter.showleaderboard() ", causes me issues @ point 1-2.

in other words how need rewrite old existing code make work in nsobject class. stuck , have being trying figure out ages. thank help.

i solved ages ago incase sees this. put these functions in gameviewcontroller showing login screen or menu instead of had in helper class.

func showgamecenterlogin() {      self.presentviewcontroller(gamecenter.sharedinstance.presentingviewcontroller, animated: true, completion: nil) }  func showgamecentermenu() {      gamecenter.showgamecentermenu(self, viewstate: .default) 

}

and calling them nsnotificationcenter when required.

to record score call function corrent score and/or leaderboard id save it, obvious , can't remember why caused me issues than.

   gamecenter.sharedinstance.savehighscore(highscore) 

Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -