我怎样才能保存高分?

时间:2015-06-13 22:23:49

标签: ios swift sprite-kit nsuserdefaults

在我得到任何帮助之前,我想澄清一下我不是专家,你会看到。我是编程的初学者。现在就说了。

如何在比赛结束后保存高分?在下面的代码中,我使用的是NSUserDefaults,但我遇到了一个无法修复的问题。一旦我运行游戏,我得到30分,一旦倒计时达到0,然后我再次尝试再次播放,我得到一个12例如它自动12是新的高分到12这是完全错误但我无法解决它。

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 for the 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                 // The + will move it to the right side and - to the left side for 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 the 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"){

            for 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")
    }

}

1 个答案:

答案 0 :(得分:1)

func saveHighScore(high:Int) {
    if high > highScore() {
        NSUserDefaults().setInteger(high, forKey: "highscore")
        println("highscore saved")
    } else {
        println("not a highscore")
    }
}