Search code examples
iosswiftnstimertouchesbegan

signal sigbart error when using NSTimer within a touches began function


Im trying to build an app and when the screen is first touched it starts an NSTimer, but using an NSTimer within the touches began function I get a signal sigbart error. If anyone has any alternate methods I could use to get the same outcome I would appreciate it.

This is my basic current code

    var condition = 0

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

    if(condition == 1) {
    timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("timerUpdate"), userInfo: nil, repeats: true)
    }

    cat.physicsBody?.velocity = CGVectorMake(0, 0)
    cat.physicsBody?.applyImpulse(CGVectorMake(0, 1))


}

Solution

  • You have to add the function timerUpdate for the selector.

     override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        if(condition == 1) {
            timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("timerUpdate"), userInfo: nil, repeats: true)
        }
    
        cat.physicsBody?.velocity = CGVectorMake(0, 0)
        cat.physicsBody?.applyImpulse(CGVectorMake(0, 1))
    }
    
    func timerUpdate(){
        print("timerUpdate")
    }