Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/107.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 斯威夫特:我如何根据还剩多少时间来制定评分系统?_Ios_Swift_Timer - Fatal编程技术网

Ios 斯威夫特:我如何根据还剩多少时间来制定评分系统?

Ios 斯威夫特:我如何根据还剩多少时间来制定评分系统?,ios,swift,timer,Ios,Swift,Timer,我有一个应用程序,有一个倒计时。我希望这样,用户完成游戏的速度越快,他们得到的分数就越多。例如,给定的总时间为120秒,因此在60秒内完成游戏的用户将获得120-60=60分,而在100秒内完成游戏的用户将获得120-100=20分 下面是我用来倒计时的代码示例 func setupGame() { seconds = 120 timerLabel.text = "Time: \(seconds)" timer = NSTimer.scheduledTimerWithTimeInterval(

我有一个应用程序,有一个倒计时。我希望这样,用户完成游戏的速度越快,他们得到的分数就越多。例如,给定的总时间为120秒,因此在60秒内完成游戏的用户将获得120-60=60分,而在100秒内完成游戏的用户将获得120-100=20分

下面是我用来倒计时的代码示例

func setupGame()  {
seconds = 120
timerLabel.text = "Time: \(seconds)"
timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: Selector("subtractTime"), userInfo: nil, repeats: true)
}

func subtractTime() {
    seconds--
    timerLabel.text = "Time: \(seconds)"
    if(seconds == 0)  {
        timer.invalidate()
        let alertController = UIAlertController(title: "You're Done!", message: "What would you like to do now?", preferredStyle: .Alert)

        let shareAction = UIAlertAction(title: "Share", style: UIAlertActionStyle.Default) {
            UIAlertAction in
            NSLog("Share Pressed")
        }

        let okAction = UIAlertAction(title: "Continue", style: UIAlertActionStyle.Cancel) {
            UIAlertAction in
            NSLog("Continue Pressed")
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let vc = storyboard.instantiateViewControllerWithIdentifier("singleViewController") as UIViewController
            self.presentViewController(vc, animated: true, completion: nil)
        }
        alertController.addAction(okAction)
        alertController.addAction(shareAction)

        self.presentViewController(alertController, animated: true, completion: nil)
    }
}
如您所见,当计时器达到0时,会弹出警报。当这种情况发生时,由于剩下的时间是0,我希望这个人得到的分数也是0。(如果你想知道人们是如何停止游戏的,这样他们就可以得到一个不是0的分数-我还有一个“完成”按钮,可以提前停止计时器并显示类似的警报)

现在,我需要的是一种捕获计时器何时停止的方法,进行120-user's time left=score的数学运算,这样我就可以在下一个ViewController的标签中显示它


请告知

假设游戏中没有“暂停”功能,一个简单的解决方案是在游戏开始时创建一个日期,然后在需要时间时检查该日期后的秒数。

谢谢您的快速回复!我的游戏没有暂停功能。这似乎是一个理想的解决方案。我该怎么做呢?我假设你有一些功能,你可以开始游戏,开始计时。无论在何处,创建一个NSDate。(首先在函数之外引用它)。每当时间停止时,检查从现在起创建的日期变量的时间间隔。结果应该是负数(因为日期早于现在),所以您只需将其添加到总秒数中即可。谢谢您的帮助。我以前从未听说过NSDate,所以我必须学会如何使用它。啊。。。新手的乐趣!