How do I make a scoring system based on how much time is left?

I have an app that has a countdown timer. I want it to be such that the faster a user completes the game, the more points they get. For example, the total amount of time given is 120 seconds, so a user who completes the game in 60 seconds gets 120 - 60 = 60 points, and a person who finishes the game in 100 seconds gets 120 -100 = 20 points.


Here is a sample of the code I am using to do my countdown timer.


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

To be completely honest, I did not write this code myself. I searched on the Internet and took bits and pieces of it to put in my app.


As you can see, when the timer reaches 0, an alert pops up. When this happens, since the time left is 0, I want the score this person gets to be 0 as well. (If you're wondering then how do people stop the game so that they will get a score that's not 0 - I also have a Done button that stops the timer earlier and shows a similar alert)


It's probably worth mentioning that my game does not have the ability to pause and I'd like it to be that way. Also worth mentioning that I am a complete newbie so please don't bite me!


Now all I need is a way to capture when the timer was stopped, do the math of 120 - user's time left= score so that I can display it in a label on the next ViewController.


Please advise!

Answered by @ben in 100893022

To me it looks like you already answered your own question. The way this is set up, the seconds variable automatically gets decremented every 1 second when the timer fires. So in your other method (the one where the user presses the Done button), you just calculate the score and display it. If you get 1 point per second remaining, you actually don't have to calculate anything, as the current value of "seconds" starts at 120, and will be equal to the final score when you stop the game.

myLabel.text = "Your score was \(seconds)."
Accepted Answer

To me it looks like you already answered your own question. The way this is set up, the seconds variable automatically gets decremented every 1 second when the timer fires. So in your other method (the one where the user presses the Done button), you just calculate the score and display it. If you get 1 point per second remaining, you actually don't have to calculate anything, as the current value of "seconds" starts at 120, and will be equal to the final score when you stop the game.

myLabel.text = "Your score was \(seconds)."
How do I make a scoring system based on how much time is left?
 
 
Q