0

I would to call from the GameViewController my timer method (written in my GameScene) in order to pause the game with an UIButton initialized in GameViewController class.

I'm trying to use a class func like this :

class GameScene: SKScene, SKPhysicsContactDelegate {
    override func didMoveToView(view: SKView) {
        GameScene.startTimer()
    }

    class func startTimer(){
        timerCount = NSTimer.scheduledTimerWithTimeInterval(1.0
        , target: self, selector: Selector("updateTimer:"), userInfo: nil, repeats: true)
    }


    func updateTimer(dt:NSTimer){

        counter--
        counterGame++

        if counter<0{
            timerCount.invalidate()
            removeCountDownTimerView()
        } else{
            labelCounter.text = "\(counter)"
        }

        if counterGame>20{
            balloon.size = CGSizeMake(50, 50)
        }
        if counterGame>40{
            self.physicsWorld.gravity = CGVectorMake(0, -0.8)
        }
        if counterGame>60{
            self.physicsWorld.gravity = CGVectorMake(0, -1)
        }
    }

    func removeCountDownTimerView(){
        defaults.setInteger(balloonDestroyed, forKey: "score")
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let settingController: UIViewController =  storyboard.instantiateViewControllerWithIdentifier("GameOverViewController") as UIViewController
        let vc = self.view?.window?.rootViewController
        vc?.presentViewController(settingController, animated: true, completion: nil)


    }

}

But this code return an error :

[Funfair_balloon.GameScene updateTimer:]: unrecognized selector sent to class 0x10b13d940

When I don't use the class func the app works perfectly but I can't stop the timer with my UIButton. What did I do wrong?

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
Haox
  • 608
  • 1
  • 7
  • 23
  • In your `class func startTimer` you tell the timer to call `updateTimer` in `timerCount = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: Selector("updateTimer:"), userInfo: nil, repeats: true)` but `self` in a class func is NOT an instance, and so you can't use it to call your `updateTimer`, which is an instance method, not a class method. – Grimxn Oct 13 '14 at 17:44
  • Thank you for your answer. I replace self by GameScene(). I don't have the error anymore but the timer is not working. – Haox Oct 13 '14 at 20:27

2 Answers2

0

Please note that when using class func, you cannot use the variables that were initialized in that class.

So for example, you cannot use the variable timerCount if it was initialized in the class GameScene.

Dejan Skledar
  • 11,280
  • 7
  • 44
  • 70
0

I'm not sure why you want to use class functions. The following should work using just the current instance of GameScene. Note that the var timerCount is an optional (since you can't easily override the init) until such times as you create it in startTimer(), and so you will have to unwrap it when you eventually invalidate it.

class GameScene: SKScene, SKPhysicsContactDelegate {
    var timerCount: NSTimer? = nil
    var counter = 100 // or whatever

    override func didMoveToView(view: SKView) {
        self.startTimer()
    }

    func startTimer() {
        self.timerCount = NSTimer.scheduledTimerWithTimeInterval(1.0
            , target: self, selector: Selector("updateTimer:"), userInfo: nil, repeats: true)
    }


    func updateTimer(dt: NSTimer) {
        // Do stuff
        counter--

        if counter < 0 {
            self.timerCount!.invalidate() // or dt.invalidate()
            self.removeCountDownTimerView()
        } else {
            // update counter label
        }
        // Do more stuff

    }

    func removeCountDownTimerView() {
        // Do stuff
    }

}
Grimxn
  • 22,115
  • 10
  • 72
  • 85
  • Thank you. Actually I have my GameScene where the game and the timer are written. In my GameViewController I created the "pause" UIButton. When I tap on the pause button I want to stop the game AND the timer. For the moment I can pause the skView (not the scene) and invalidate `timerCount` with this button but the nodes continue to be created and I can't start again the timer. That's why I want to access the methods present in GameScene from the ViewController. – Haox Oct 14 '14 at 09:52
  • Well I just tried your code. It works but how can I restart the timer from the ViewController with the `pauseButton`? – Haox Oct 14 '14 at 19:00
  • Make the `startTimer` an action, and connect it to your button? – Grimxn Oct 15 '14 at 20:11
  • Ok I Will try that. (I'm still a noob) :-D – Haox Oct 15 '14 at 20:17