-1

I want to make a app, where you are getting 1 point each second, and it all works fine. But since i have multiple view controllers, then timer runs again, and gives 2 points per second. How can i fix that?

The code:

//
//  ViewController.swift
//  TapTap
//
//  Created by Jonas Boutrup on 10/02/2016.
//  Copyright © 2016 Jonas Boutrup. All rights reserved.
//

import UIKit
import RealmSwift
import Foundation

class ViewController: UIViewController {

    //initialize your realm
    let realm = try! Realm()

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.darkGrayColor()
        loadpoints()
        startAddPoints()
        print(Realm.Configuration.defaultConfiguration.path!)
    }

    @IBOutlet weak var pointLabel: UILabel!
    @IBOutlet weak var bonusPointLabel: UILabel!

    var points = 0
    var bonusPoints = 0
    var multiplierClick = 0
    var pointsPerSecond = 1
    var timer = NSTimer()

    func loadpoints() {

        //check if there is a Click with id == 1
        if let clickWithId1 = realm.objects(Click).filter("id == 1").first {
            self.points = clickWithId1.totalPoints
            self.bonusPoints = clickWithId1.totalBonusPoints
            self.multiplierClick = clickWithId1.multiplierClick
            self.pointsPerSecond = clickWithId1.pointsPerSecond
        } else {  // if there is no Click with id == 1, create it and add it to realm
            let click = Click()
            click.id = 1
            click.totalBonusPoints = 0
            click.totalPoints = 0
            click.multiplierClick = 1
            click.timesTwoPrice = 20
            click.pointsPerSecond = 1
            self.points = click.totalPoints
            self.bonusPoints = click.totalBonusPoints

            do {
                try realm.write {
                    realm.add(click, update: true)
                }

            } catch {}
        }
        self.pointLabel.text = "\(points)"
        self.bonusPointLabel.text = "\(bonusPoints)"
    }

    func startAddPoints() {
        self.timer = NSTimer.scheduledTimerWithTimeInterval(1, target:self, selector: Selector("addPoints"), userInfo: nil, repeats: true)
    }



    func addPoints() {
        //check if there is a Click with id == 1
        if let clickWithId1 = realm.objects(Click).filter("id == 1").first {

            do {
                //if there is a Click with id == 1, increment its totalclicks by 1 and update it

                try realm.write {
                    clickWithId1.totalPoints += clickWithId1.pointsPerSecond
                    realm.add(clickWithId1, update: true)
                }

            } catch {}

            loadpoints()

        }
    }

    @IBAction func buttonTapped(sender: AnyObject) {

        //check if there is a Click with id == 1
        if let clickWithId1 = realm.objects(Click).filter("id == 1").first {

            do {
                //if there is a Click with id == 1, increment its totalclicks by 1 and update it

                try realm.write {
                    clickWithId1.totalPoints += clickWithId1.multiplierClick
                    realm.add(clickWithId1, update: true)
                }

                self.points = clickWithId1.totalPoints
                self.pointLabel.text = "\(points)"
                let roll = arc4random_uniform(100) + 1
                print("\(roll)")

                if roll == 14 {
                    try realm.write {
                        clickWithId1.totalBonusPoints += 1
                        self.bonusPoints = clickWithId1.totalBonusPoints
                        self.bonusPointLabel.text = "\(bonusPoints)"
                        realm.add(clickWithId1, update: true)
                    }
                }

            } catch {}
        }
    }
}

Thanks in advance! :D

EDIT

I found a solution to it! You need to have a button that stops the timer!

@IBAction func stopTimer(sender: AnyObject) {  
        timer.invalidate()   
    }
1

1 Answer 1

1

Best would be call the timer from one place only (for example another class which is not a VC and accessible globally or app delegate). Another option could be to invalidate a timer every time you leave a view controller, so there is only one timer that works at a time. Yet another option could be to have an instance of NSTimer passed as a parameter in the addPoints function so you can check for a particular timer object. There is a number of ways, but ideally you would have only one timer.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.