Reputation: 163
I am using Swift to develop an app that uses timer so i am using this code for timer
var timer = NSTimer()
timer=NSTimer.scheduledTimerWithTimeInterval(1.0,target:self,selector:Selector("updateTimeLabel"), userInfo:nil , repeats: true)
my problem is this when this game complete a view appears as subview of current view and the timer invalidates using
timer.invalidate()
and on this subview there is a button to start a new game when this button is pressed a new game is started but the timer starts from the time it stopped.
suppose the game finished in 00:10 time so the subview appears and if we start new game then the time will start from 00:11. This works fine when user moves to another view and then comes back to the same view for game play, in this case the timer starts from 00:00 pls tell where i am wrong
THANKS IN ADVANCE.
Upvotes: 2
Views: 1679
Reputation: 1174
reset your sec and minute variable that are updating the label in your updateTimeLabel method. this would do
Upvotes: 1
Reputation: 13651
Your timer is only used to fire the method that will update the time label.
You need to reset the variable that holds the elapsed time to zero when the game finishes. Probably timeSec
in your case, which is hold by your GameScreen
class.
Upvotes: 0
Reputation: 1933
You should reset your timeSec and timeMin variables when he decides to start a new game.
In other words, whenever that subview with the button is removed from its superview, reset it.
Or, do it into that button action.
Upvotes: 2