在iOS中将高分设置为已用时间
我游戏中的高分是基于时间的,我很难将它与当前分数进行比较。这是我到目前为止不起作用的代码:在iOS中将高分设置为已用时间,ios,swift,sprite-kit,Ios,Swift,Sprite Kit,我游戏中的高分是基于时间的,我很难将它与当前分数进行比较。这是我到目前为止不起作用的代码: class GameScene: SKScene, SKPhysicsContactDelegate { // initialise value for current time var currentTime = NSDate() var bestTime = NSDate() override func didMoveToView(view: SKView) { super.didMov
class GameScene: SKScene, SKPhysicsContactDelegate {
// initialise value for current time
var currentTime = NSDate()
var bestTime = NSDate()
override func didMoveToView(view: SKView) {
super.didMoveToView(view)
// set the current time to 0 seconds
var date0 = NSDate();
let timeInterval = floor(date0 .timeIntervalSinceReferenceDate / 60.0) * 60.0
date0 = NSDate(timeIntervalSinceReferenceDate: timeInterval)
currentTime = date0
// call to start timer
NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "printDuration:", userInfo: NSDate(), repeats: true)
然后
我希望能够执行如下操作,其中我能够比较两个变量中的时间,并相应地设置较高的时间:
if (currentTime > highScore) {
highScore = currentTime
highScoreLabel.text = "\(NSString(format:"%3.2f", highScore))"
}
您可以在开始时在didMoveToView()中设置startDate=NSDate(),然后通过键入来比较两者的持续时间是否更高
if duration > highScore {
highScore = duration
}
尝试运行此代码时会发生什么情况?date0的值变为:-1717426878592353655261912811698550882625289000250446469916584656642821486237650977479701259456311859300382927995518854639197121019398242881322948354109005732550749122382004224.00启动并测试时只需保存
startDate=NSDate()
NSDate().timeIntervalSinceDate(startDate)谢谢,startDate变为0.000379025936126709,但是如何比较这两个NSDate().timeIntervalSinceDate(startDate)?这将为您提供一个时间间隔(持续时间)。设置highscore=NSDate()并尝试进行此比较后,我得到了错误“二进制运算符'>'不能应用于'NSInterval'(又名Double)和NSDate类型的操作数。highscore应该是一段时间吗?是的,玩家最后一次将其高分变为该时间长度(5.01s)的时间越长,您就不希望highscore为NSDate()。您可以让swift找出类型,也可以显式地将其设置为NSInterval。就是这样。谢谢
if duration > highScore {
highScore = duration
}