sprite kit - Swift game not updating highscore inside didMoveToView -
i'm creating game swift , score works fine. problem highscore. created function called checkscores() check if score greater highscore. highscore remains @ 0.
func checkscores() { if scoreint > highscoreint { nsuserdefaults.standarduserdefaults().setinteger(scoreint, forkey: "high") highscoreint = nsuserdefaults.standarduserdefaults().integerforkey("high") nsuserdefaults.standarduserdefaults().synchronize() highscorelbl.text = "highscore : \(highscoreint)" }
i called inside didmovetoview. created highscore label , integer inside game scene, , set label text inside inithud()
func inithud() { scorelbl = sklabelnode(fontnamed: "geneva") scorelbl.position = cgpointmake(self.frame.size.width / 2, self.frame.size.height / 2) scorelbl.text = "0" scorelbl.fontsize = 100 scorelbl.alpha = 0.5 addchild(scorelbl) highscoreint = nsuserdefaults.standarduserdefaults().integerforkey("high") nsuserdefaults.standarduserdefaults().synchronize() highscorelbl = sklabelnode(fontnamed: "geneva") highscorelbl.fontsize = 30 highscorelbl.position = cgpointmake(150, self.frame.size.height - 80) highscorelbl.text = "highscore : \(highscoreint)" addchild(highscorelbl) }
when run game highscore remains @ zero. tried put checkscore() inside didbegincontact , works fine. why? because physics updated every frame? possible check inside didmovetoview? thanks!
place code inside update()
var highscore = 1 let userdefaults = nsuserdefaults.standarduserdefaults() highscore = userdefaults.integerforkey("highscore") userdefaults.setvalue(highscore, forkey: "highscore") if scorecount > highscore { userdefaults.setvalue(scorecount, forkey: "highscore") } highscorelabel.text = "highscore: \(highscore)" userdefaults.synchronize()
Comments
Post a Comment