Quote:
Originally Posted by iabaksardna
Hey guys,
I'm trying to make a game in which the following statement is usd:
Code:
[NSTimer scheduledTimerWithTimeInterval:0.001 target:self selector:@selector(gameLoop) userInfo:nil repeats:YES];
Instead of the 0.001, I want to have a variable that can be modified by the user. Can anyone help?
Thanks, Andy
|
A couple of things:
NSTimers are fairly coarse. To quote from the XCode docs:
A timer is not a real-time mechanism; it fires only when one of the run loop modes to which the timer has been added is running and able to check if the timer’s firing time has passed. Because of the various input sources a typical run loop manages, the
effective resolution of the time interval for a timer is limited to on the order of 50-100 milliseconds. If a timer’s firing time occurs during a long callout or while the run loop is in a mode that is not monitoring the timer, the timer does not fire until the next time the run loop checks the timer. Therefore, the actual time at which the timer fires potentially can be a significant period of time after the scheduled firing time.
You won't get called anywhere near every .001 seconds. More like every .05 seconds, and don't expect the calls to be exact. There is quite a bit of variability in the time between calls, and the system may miss a timer interval completely sometimes.
The other thing is that you can't change the timer interval for a running repeating timer, at least not that I know of.
You are probably better off creating a one-shot timer, and when it goes off, create a new one. Then you can pick up a user-specified interval for the next timer you create.