Timer goes to negatives / does not resell

I am trying to set a countdown timer for my application. My code works so that every time the whiteDot image is dragged and contains a smallDot, a countdown starts and the smallDot is generated at a random position on the screen. I have a couple of questions.

1.) I'm trying to get the timer to reset to 2 seconds after each time the if if (whiteDot.frame.contains (smallDot.frame) & smallDot.image! = Nil) statement is executed.

2.) Every time the "if" statement is executed once, it usually counts down, but when it is executed again before the countdown reaches zero, it starts going to negative numbers and counting in less than 1 second.

import UIKit

var timeClock =  2

class SecondViewController: UIViewController {

func startTimer() {
    timeClock -= 1
    time.text =  "Time: " + String(timeClock)

if whiteDot.frame.contains(smallDot.frame) && timeClock > 0 {
        timeClock = 2
    }

   else if timeClock == 0 || timeClock < 0 {
        timer.invalidate()
  } 
 }

var timer = Timer()

@IBAction func handlePan(recognizer:UIPanGestureRecognizer) {
    let translation = recognizer.translation(in: self.view)
    if let view = recognizer.view {
        view.center = CGPoint(x:view.center.x + translation.x,
                              y:view.center.y + translation.y)
    }
    recognizer.setTranslation(CGPoint.zero, in: self.view)

    if (whiteDot.frame.contains(smallDot.frame) && smallDot.image != nil) {
        addOne += 1

        score.text = "\(addOne)"

        smallDot.center = spawnRandomPosition()

timeClock = 2
        if timeClock == 0 || timeClock < 0 {
            timer.invalidate()
        }
        else if timeClock > 0 && (whiteDot.frame.contains(smallDot.frame)){
            timeClock = 2

        }
        timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(SecondViewController.action), userInfo: nil, repeats: true)  
    }
    }

      

+3


source to share


2 answers


Just figured out my problem. I didn't rewrite timer to timer at all. At first I tried to use reset on the "action" function, but I just had to move it down to the operator to solve all my problems. This code works for anyone trying to reset their timer when some action happens.



class SecondViewController: UIViewController
{
 var addOne = 0

func startTimer() {

    timeClock -= 1

    time.text =  "Time: " + String(timeClock)

   if timeClock <= 0 {
    timer.invalidate()
    //show game over
    }
}

func resetTimer() {
    timer.invalidate()
    timeClock = 3
}
var timer = Timer()

 @IBAction func handlePan(recognizer:UIPanGestureRecognizer) {
    let translation = recognizer.translation(in: self.view)
    if let view = recognizer.view {
        view.center = CGPoint(x:view.center.x + translation.x,
                              y:view.center.y + translation.y)
    }
    recognizer.setTranslation(CGPoint.zero, in: self.view)

    if (whiteDot.frame.contains(smallDot.frame) && smallDot.image != nil) {
        addOne += 1

  score.text = "\(addOne)"

        resetTimer()

        timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(SecondViewController.startTimer), userInfo: nil, repeats: true)

        smallDot.center = spawnRandomPosition()   
    }
    }

      

+1


source


var timeClock =  2

      

Since this appears inside the class, but outside of any method, this line declares a property for your class named timeClock

and initializes it to 2

. This will be the initial value for the property timeClock

for any instance of the class you create.



You are updating this value correctly as you count, but nowhere are you resetting to 2

. Entering a property declaration below a method action()

does not mean that it will be executed after that. The property is initialized only once per instance.

What you want to do is either set the value timeClock

to 2

before you schedule the timer or set it to 2

after you canceled the timer. You need to reset it to anyway 2

.

0


source







All Articles