Pause CFTimeInterval Timer? - ios

In my game I have a timer in my update loop that updates a label based upon the interval returned in this line:
elapsedTime = 90 - (CFAbsoluteTimeGetCurrent() - startTime);
Now 90 is the time we tick down from. I set startTime like so:
startTime = CFAbsoluteTimeGetCurrent();
Now what I am aiming to do is "pause" this timer when I go to my pause menu. However I just cannot wrap my head around the logistics of it.
When I pause should I set startTime to 0? If so, what would I do during the resume method?
Basically if the user pauses the game with 85 seconds to go, I want the user to come back from the pause screen with 85 seconds to go still. How would I achieve this?

I got my answer. I just did what I was doing now but I included another timer for the paused screen and I just add it up continually when paused and then I incorporate that timer into my game timer and it worked out.

You could store the amount of time, that passed since the timer started.
With NSDate you can store the startTime of the timer and than use -timeIntervalSinceNow to retrieve the time that passed.

Related

Get remaining time of Timer in Dart

How to get the remaining time in seconds of Timer from 'dart:async' in Dart?
I have a countdown timer and at some point I want to get the remaining time in seconds. How do I do this in dart?
_endTimer = Timer(Duration(seconds: totalRemainingTimeInSeconds), () {
_end();
});
You cannot get that from a Timer.
What you can do is to start a Stopwatch at the same time as the timer.
If you need to do things along the way, they can check the stopwatch to see the duration so far. To update a countdown timer, I'd probably use a combination of Stopwatch to count time and Timer.periodic to update the UI occasionally, and either have the periodic timer check whether the countdown is over, or have a single Timer to react on timeout.

how to implement a timer (including milliseconds) into an app

So I am creating a basic game for iOS (using exclusively objective-c) Xcode and have looked all over but cannot seem to find any tips for a starting point. I just want to time the player while the game is playing and have the scene all set up but can't figure out how to generate a timer just to display the time as the player plays and then pass it to my game over scene when the game finishes.
any tips or help what so ever would be greatly appreciated.
There are 2 parts to this. One part is to have a repeating event to display new info (elapsed time) to the user. For that NSTimer is perfect. See the comments from trojanfoe and tnev for info on NSTimer.
The second part is calculating the amount of time that's elapsed.
For that I recommend the following:
Create an instance variable of type NSTimeInterval (a double) in your view controller class:
#property (nonatomic, assign) NSTimeInterval startTime;
In your game start method, save the current time interval:
-(void) gameSart
{
self.startTime = [NSDate timeIntervalSinceReferenceDate];
//Also start an NSTimer to display elapsed time.
}
Then code to calculate elapsed time (or total time):
NSTimeInterval elapsed =
[NSDate timeIntervalSinceReferenceDate] - self.startTime; //in seconds
//Display elapsed time to a label, perhaps formatted as minutes/seconds.

How to keep the NSTimer always work evenly

Now I am creating a metronome program. I use a NSTimer to bring the metronome into play. Of course The Timer works repeatedly. But I find out in two situation that the timer works not accruately.
When just start the NSTimer, the first two beat sometimes goes too closely. After then, the beat goes evenly.
When the app goes backgound I make the Timer work continuely by:
[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL];
but sometimes the unevenly beat also happens at the time when entering or coming back from the background status.
So I want to know how to keep the Timer always work evenly, no matter which situation it is in. Thks!
When setting up a timer firing at a certain time, the system implicitly sets an allowed "leeway" which is a short delay within the timer may actually fire.
This is due to "Timer Coalescing" - a system feature - which groups events happening roughly at the same time together and fire them at the exact same time. The reason is to reduce CPU cycles and extend the idle time of the CPU to save power.
If the timer interval is small (milli seconds to seconds) this "leeway" is in the range of 10% of the timer interval.
You can explicitly retrieve and set the current maximum allowed leeway value with the methods
-tolerance
-setTolerance:
See also:
NSTimer
Timing Accuracy

NSTimer start delay

I have an UIButton that triggers a NSTimer with 1 sec interval, it calls a selector a countdown, this countdown is shown in the screen. We have detected that the NSTimer does not start immediately, it costs one second aprox. to begin. Is there anyway to force NSTimer to start immediately?
Thanks!
NSTimer has a fire method. call it after initialization.
check the documentation here https://developer.apple.com/library/mac/documentation/cocoa/reference/foundation/classes/NSTimer_Class/Reference/NSTimer.html#//apple_ref/occ/instm/NSTimer/fire

How to Make and automatic timer reset in Delphi 7?

Could someone tell me how to make a timer Reset after a given time ? Like i'm trying to make a clock , and i would like it to reset , and start over (like after 60 min , and start over from 0).
Have your OnTimer event update the timer's Tag property, or a separate variable, with how much time has elapsed, and then reset the timer when that value reaches your threshold.
Or you could simply run a separate timer that triggers one time after your desired interval elapses, and then reset both timers together.

Resources