I'm creating an iOS test game as practice and want a game over method to be ran at the end of a 15 second timer.
I also want time to be added to this timer and taken away if certain actions take place.
Not having much luck figuring out how to add time or take away time from a certain timer depending on actions by the user.
So far I have the timer in viewDidLoad
and a method with some conditional formatting that will do things depending on what the user does.
Once it has been created, the period of an
NSTimer
cannot be changed, the timer can only be cancelled or allowed to fire after the requested period.In order to implement a timer for your game I would suggest that you set up a repeating timer that fires every second. The actual 'time remaining' value is stored in an integer and you decrement this value each time the timer fires. When this value reaches 0, end the game.
This way you can easily add additional time by simply changing the value of the time remaining variable.
This approach also makes it simple to display the remaining time on the screen.