ios - how can i save the high score? -


before want clarify im not expert you'll see. i'm complete beginner in programming. said.

how can manage save high score after game ends? in following code i'm using nsuserdefaults i'm having problem in can't fix. once run game , score of 30 once countdown hits 0 example , try play again , 12 example automatically 12 new high score 12 totatly wrong can't fix it.

class game: skscene, skphysicscontactdelegate {      let ball = skspritenode(imagenamed: "red.png")      var quitoption = sklabelnode()     var scorelabel = sklabelnode()      var timesecond = int(60)      var locked = false      var loseoption = sklabelnode()     var scorepoints = sklabelnode()       var score = int()       let whiteball = skspritenode(imagenamed: "fingerpointingdown.png")      var caseforscoreone = sklabelnode()     var caseforscoretwo = sklabelnode()       struct physicscategory {         static let ball: uint32 = 0b1         static let whiteball: uint32 = 0b10     }       override func didmovetoview(view: skview) {           backgroundcolor = skcolor.whitecolor() // background display          self.physicsworld.gravity = cgvectormake(0, -9.8)         self.physicsworld.contactdelegate = self          let scenebody = skphysicsbody(edgeloopfromrect: self.frame)         scenebody.friction = 0         self.physicsbody = scenebody          scorepoints = sklabelnode(fontnamed: "noteworthy-light")         scorepoints.text = "0"         scorepoints.fontcolor = skcolor.blackcolor()         scorepoints.fontsize = 35         scorepoints.position = cgpoint(x: self.frame.size.width/2, y: self.frame.size.height*1 - 120)         scorepoints.name = "points"           addchild(scorepoints)          ball.size = cgsize(width: 82, height: 82)         ball.position = cgpoint(x: self.frame.size.width/2, y: self.frame.size.height*0.1 - 60)         ball.physicsbody = skphysicsbody(circleofradius: 41)         ball.physicsbody?.affectedbygravity = true         ball.physicsbody?.density = 10         ball.physicsbody?.restitution = 0.1         ball.physicsbody?.lineardamping = 0         ball.name = "ball"         ball.physicsbody?.usesprecisecollisiondetection = true         ball.physicsbody?.categorybitmask = physicscategory.ball         ball.physicsbody?.contacttestbitmask = physicscategory.whiteball         ball.physicsbody?.collisionbitmask = physicscategory.whiteball          self.addchild(ball)          quitoption.text = "quit"         quitoption.fontname = "noteworthy-light"         quitoption.fontcolor = skcolor.purplecolor()         quitoption.fontsize = 35         quitoption.position = cgpoint(x: self.frame.size.width/2 - 160, y: self.frame.size.height*1 - 110)         quitoption.name = "quit"          addchild(quitoption)          scorelabel = sklabelnode(fontnamed: "noteworthy-light")         scorelabel.fontcolor = skcolor.redcolor()         scorelabel.fontsize = 35                 // + move right side , - left side more accuracy.         scorelabel.position = cgpoint(x: self.frame.size.width/2 + 160, y: self.frame.size.height/1 - 115) // position of scorelabelnode         scorelabel.name = "score+"         scorelabel.hidden = false          self.addchild(scorelabel)          whiteball.size = cgsize(width: 55, height: 55)         whiteball.position = cgpoint(x: self.frame.size.width/2, y: self.frame.size.height*0.8 - 30)         whiteball.name = "whiteball"         whiteball.physicsbody = skphysicsbody(circleofradius: 25)         whiteball.physicsbody?.dynamic = false         whiteball.physicsbody?.restitution = 0.1         whiteball.physicsbody?.usesprecisecollisiondetection = true         whiteball.physicsbody?.categorybitmask = physicscategory.whiteball         whiteball.physicsbody?.contacttestbitmask = physicscategory.ball         whiteball.physicsbody?.collisionbitmask = physicscategory.ball          self.addchild(whiteball)           caseforscoreone = sklabelnode(fontnamed: "noteworthy-light")         caseforscoreone.fontcolor = skcolor.blackcolor()         caseforscoreone.fontsize = 25         caseforscoreone.position = cgpoint(x: self.frame.size.width/2 - 160, y: self.frame.size.height*1 - 175)          caseforscoreone.name = "caseone"          addchild(caseforscoreone)          caseforscoretwo = sklabelnode(fontnamed: "noteworthy-light")         caseforscoretwo.fontcolor = skcolor.blackcolor()         caseforscoretwo.fontsize = 25         caseforscoretwo.position = cgpoint(x: self.frame.size.width/2 + 160, y: self.frame.size.height/1 - 175)          caseforscoretwo.name = "casetwo"          addchild(caseforscoretwo)      }      // making ball jump after user touches ball      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 == "quit"){              let myscene = gamescene(size: self.size)             myscene.scalemode = scalemode             let reveal = sktransition.fadewithduration(1)             self.view?.presentscene(myscene, transition: reveal)          }          if (node.name == "ball"){              touch: anyobject in touches {                   ball.physicsbody?.allowsrotation = true                 ball.physicsbody?.velocity = cgvectormake(0, 0)                 ball.physicsbody?.applyimpulse(cgvectormake(0, 450))              }          }          if(!self.locked){              self.locked = true              var actionrun = skaction.waitforduration(0.5)             var actionwait = skaction.runblock({                  self.timesecond--                  if self.timesecond == 60 {self.timesecond = 0}                  self.scorelabel.text = "\(self.timesecond)"                  if (self.timesecond == 0){                     self.savehighscore(0)                      if self.score > self.highscore() {                          self.savehighscore(self.score)                          self.caseforscoretwo.text = "n.h.s = " + self.highscore().description                      } else if (self.score > self.highscore()) {                          self.caseforscoreone.text = "n.h.s = " + self.score.description                     }                    }              })               let loopaction = skaction.repeataction(skaction.sequence([actionwait, actionrun]), count: 60)              scorelabel.runaction(loopaction, withkey: "scoreaction")           }       }      func didbegincontact(contact: skphysicscontact) {          let collision = contact.bodya.categorybitmask | contact.bodyb.categorybitmask          if collision == physicscategory.ball | physicscategory.whiteball {              score++              scorepoints.text = "score: \(score)"          }      }      func savehighscore(high:int) {         nsuserdefaults.standarduserdefaults().setinteger(high, forkey: "highscore")     }     func highscore() -> int {         return nsuserdefaults.standarduserdefaults().integerforkey("highscore")     }     func resethighscore() {         nsuserdefaults.standarduserdefaults().removeobjectforkey("highscore")     }  } 

func savehighscore(high:int) {     if high > highscore() {         nsuserdefaults().setinteger(high, forkey: "highscore")         println("highscore saved")     } else {         println("not highscore")     } }  

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -