My program has a loop that does calculation and drawing like 60 times a second. I want my movement action be executed only once a second, how do I do that with objective-c methods?
Asked
Active
Viewed 1,743 times
3 Answers
3
You can use
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds target:(id)target selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)repeats
For one second:
NSTimer *yourTastTimer = scheduledTimerWithTimeInterval:1 target:yourTarget selector:@selector(yourMethod) userInfo:nil repeats:YES;
You can check the documentation for NSTimer for details.
Edit
You may also want to check this answer.

Community
- 1
- 1

Bruno Ferreira
- 942
- 9
- 22
2
If you are using Cocos2D for iphone you MUST NOT use the NSTimer.
Following the documentation, you should use the schedule:interval:
method like that:
[self schedule: @selector(tick:) interval:1.0f];
- (void) tick: (ccTime) dt
{
//...
}

Yannick Loriot
- 7,107
- 2
- 33
- 56
-
1Well, you __can__ use NSTimer, although it is highly discouraged :) – tallen11 Aug 07 '12 at 18:43
0
If your using a CCSprite ,they have a schedule method which might do the trick. Basically do what for the main loop. You could also keep a ticker in your main loop and check that this is the ticker%60==0. Then call your movement action.
I read NSTimer is discouraged with cocos2d here.