如何快速保存高分
Posted
技术标签:
【中文标题】如何快速保存高分【英文标题】:How to save highscore swift 【发布时间】:2015-10-26 20:50:57 【问题描述】:长期以来,我一直在尝试和寻找对我有帮助的东西,但我还没有找到解决方案。我只是想为我正在制作的游戏保存一个高分,但它总是没有显示任何错误,然后在我尝试运行时给我一些奇怪的错误,比如“线程 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】:试试这个代码
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()
【讨论】:
我把它放进去,但它给了我一个错误,说 highScoreLabel.text = "highscore: (highscore)" 有这个错误:EXC_BAD_INSTRUCTION 有什么想法吗? @MattTaylor 不确定。我自己试过了,效果很好。我将编辑我的答案以准确显示我做了什么 有效!太感谢了!我把我的用户默认值放在了错误的函数中。再次感谢您的帮助!以上是关于如何快速保存高分的主要内容,如果未能解决你的问题,请参考以下文章