ios - How can I save the high score in sprite kit using swift? -
this question has answer here:
im complete beginner @ programming. in following code want save high score , can't manage that. you'll i'm using nsuserdefaults doesn't show in screen once game over. appreciated. want high score show on screen , doesn't.
class wt: skscene {
let showmessage = sklabelnode() let tryagain = sklabelnode() var highscore = sklabelnode() override func didmovetoview(view: skview) { backgroundcolor = skcolor.blackcolor() showmessage.fontname = "noteworthy-light" showmessage.fontsize = 55 showmessage.fontcolor = skcolor.redcolor() showmessage.text = "time's up!" showmessage.position = cgpoint(x: self.frame.size.width/2, y: self.frame.size.height*0.7) showmessage.name = "show message" showmessage.hidden = false showmessage.zposition = 100 self.addchild(showmessage) tryagain.fontname = "noteworthy-light" tryagain.fontsize = 44 tryagain.fontcolor = skcolor.greencolor() tryagain.text = "try again!" tryagain.position = cgpoint(x: self.frame.size.width/2, y: self.frame.size.height/2 - 200) tryagain.name = "t.a" self.addchild(tryagain) highscore = sklabelnode(fontnamed: "noteworthy-light") highscore.text = "0" highscore.fontcolor = skcolor.whitecolor() highscore.fontsize = 35 highscore.position = cgpoint(x: self.frame.size.width/2, y: self.frame.size.height/2) highscore.name = "hs" addchild(highscore) var actionone = skaction.scaleby(0.9, duration: 0.3) var actionsequence = skaction.sequence([actionone, actionone.reversedaction(), actionone, actionone.reversedaction()]) tryagain.runaction(skaction.repeatactionforever(actionsequence)) } override func touchesbegan(touches: set<nsobject>, withevent event: uievent) { var touch = touches.first as! uitouch var location = touch.locationinnode(self) var node = self.nodeatpoint(location) if (node.name == "t.a"){ let myscene = game(size: self.size) myscene.scalemode = scalemode let reveal = sktransition.fadewithduration(1) self.view?.presentscene(myscene, transition: reveal) } } func savehighscore(highscore:int){ if let currenthighscore:int = nsuserdefaults.standarduserdefaults().valueforkey("highscore") as? int{ if(highscore > currenthighscore){ nsuserdefaults.setvalue(highscore, forkey: "highscore") } }else{ nsuserdefaults.setvalue(highscore, forkey: "highscore") } }
}
you might want synchronize settings after changing them write modifications disk. synchronize apple
nsuserdefaults.standarduserdefaults().synchronize()
Comments
Post a Comment