如何保存高分迅捷

时间:2015-10-26 20:50:58

标签: swift save nsuserdefaults

我一直在努力寻找能帮助我很长时间的东西,但我还没有找到解决方案。我只是试图为我正在制作的游戏保存高分,但它总是显示没有错误,然后当我尝试并运行时,给我一些奇怪的错误,如“线程1:信号SIGABRT”。谁能帮我看看我做错了什么?

import CoreMotion
import SpriteKit

enum CollisionTypes: UInt32{
case Player = 1
case Wall = 2
case Star = 4
case Vortex = 8
case Finish = 16
}

var levelsPassed = 0
var levelName = "level1"

class GameScene: SKScene, SKPhysicsContactDelegate {

var player: SKSpriteNode!

//hack so that we can test on the computer and then use on devises as well
var lastTouchPosition: CGPoint?

//create the motion manager
var motionManager: CMMotionManager!


//score labels
var scoreLabel: SKLabelNode!
var score: Int = 0 {
    didSet {
        scoreLabel.text = "Score: \(score)"
    }

}

var highScoreLabel: SKLabelNode!
var highestScore:Int = 0

var gameOver = false

override func didMoveToView(view: SKView) {
    /* Setup your scene here */
    UIApplication.sharedApplication().idleTimerDisabled = true
    let background = SKSpriteNode(imageNamed: "background.jpg")

    background.position = CGPoint(x: 512, y: 384)
    background.zPosition = -1
    background.blendMode = .Replace
    addChild(background)

    NSUserDefaults.standardUserDefaults().setObject(highestScore, forKey:"HighestScore")
    NSUserDefaults.standardUserDefaults().synchronize()
    if (NSUserDefaults.valueForKey("HighestScore") != nil){
        var savedScore: Int = NSUserDefaults.standardUserDefaults().objectForKey("HighestScore") as! Int
    }


    //call the loadLevel funciton
    loadLevel(levelName)
    createPlayer()

    scoreLabel = SKLabelNode(fontNamed: "Chalkduster")
    scoreLabel.text = "Score: 0"
    scoreLabel.horizontalAlignmentMode = .Left
    scoreLabel.position = CGPoint(x:32, y:100)
    scoreLabel.zPosition = 2
    addChild(scoreLabel)


    highScoreLabel = SKLabelNode(fontNamed: "Chalkduster")
    highScoreLabel.text = "Highscore: \(highestScore)"
    highScoreLabel.horizontalAlignmentMode = .Left
    highScoreLabel.position = CGPoint(x:225, y:100)
    highScoreLabel.zPosition = 2
    addChild(scoreLabel)

    //give the game no gravity by default
    physicsWorld.gravity = CGVector(dx: 0, dy: 0)
    physicsWorld.contactDelegate = self


    motionManager = CMMotionManager()
    motionManager.startAccelerometerUpdates()



}

1 个答案:

答案 0 :(得分:0)

试试此代码

  import SpriteKit

class GameScene: SKScene {

    var highScoreLabel: SKLabelNode!

    var highestScore:Int = 0

    var score = 0

    override func didMoveToView(view: SKView) {

        highScoreLabel = SKLabelNode(fontNamed: "Chalkduster")
        highScoreLabel.text = "Highscore: \(highestScore)"
        highScoreLabel.horizontalAlignmentMode = .Left
        highScoreLabel.position = CGPoint(x:225, y:100)
        highScoreLabel.zPosition = 2
        addChild(highScoreLabel)

    }

    override func update(currentTime: NSTimeInterval) {

        var highscore = 1

        let userDefaults = NSUserDefaults.standardUserDefaults()

        highscore = userDefaults.integerForKey("highscore")

        userDefaults.setValue(highscore, forKey: "highscore")

        if score > highscore {
            userDefaults.setValue(score, forKey: "highscore")

        }

        highScoreLabel.text = "highscore: \(highscore)"

        userDefaults.synchronize()


    }

}