swift - 如何快速保存高分

标签 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()



}

最佳答案

试试这个代码

  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()


    }

}

关于swift - 如何快速保存高分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33355366/

相关文章:

arrays - TextField 作为应用程序启动时的变量粘贴到 LocalNotification 中

java - Android:BitmapFactory 返回 java.io.FileNotFoundException 错误,即使图像已保存到外部存储

iphone - iOS - 在哪里保存敏感数据 - NSUserDefaults,钥匙串(keychain),还有其他什么?

swift 无法将类型 'Int' 的值分配给类型 'String'

ios - 你如何在 Swift 中锁定一个线程?

python - 如何从 python 2.7 的列表中选择名称

ios - Swift 中 NSUserDefaults 的安全性

ios - 如何将自定义类快速保存到 NSUserDefaults 中?

arrays - 检查字典中的数组并返回 UIImage 但它返回相同图像的两倍

javascript - 当用户下载文件时在另一台服务器上重命名文件