I am trying to create a new NSTimer object to simply increment my counter by 1sec (To calculate the lifetime of my game) now I've seen demos and examples on how to go about this, and have done the same thing but the project continues to fail. Any suggestions to there?
This is the error message :
unrecognized selector sent to instance 0x7a83c7d0
import SpriteKit
var timerObject = NSTimer()
var count = 0
let timer = SKLabelNode(text: "0")
class GameScene: SKScene , SKPhysicsContactDelegate {
override func didMoveToView(view: SKView) {
timerObject = NSTimer.scheduledTimerWithTimeInterval( 1 , target: self , selector: Selector("result"), userInfo: nil, repeats: true)
func result(){
count++
timer.text = String(count)
}
I also wrote the function "result" as such and still crashed
func result() {
count+= 0
timer.text = "\(count)"
}
I came up with the answer for all those experiencing the same problem , it was I was experiencing the Error message: unrecognized selector sent to instance 0x7a83c7d0 well the solution is quite simple actually , I had initialized my timerObject object inside viewDidLoad more specifically the error states that the method result is not a part of gameScene simply move the selector function of your choice underneath viewDidLoad and it should work just fine
class GameScene: SKScene , SKPhysicsContactDelegate {
var timer = NSTimer()
var count = 0
let lableTimer = SKLabelNode(text: "0")
override func didMoveToView(view: SKView) {
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "result", userInfo: nil, repeats: true)
lableTimer.position = timerPosition
lableTimer.fontSize = 40
self.addChild(lableTimer)
}
func result(){
count++
lableTimer.text = String(count)
}
Also I've moved all my initial variables inside the GameScene class rather then on top.