views:

75

answers:

3
- (void) applicationDidFinishLaunching:(UIApplication *)application
   {
    //set up main loop 
    [NSTimer scheduledTimerWithTimeInterval:0.033
    target:self selector:@selector(gameLoop:) userInfo:nil repeats:NO];
    //create instance of the first GameState 
    [self doStateChange:[gsMain class]];
   }
 - (void) gameLoop: (id) sender
   {
    [((GameState*)viewController.view) Update]; 
    [((GameState*)viewController.view) Render];
    [NSTimer scheduledTimerWithTimeInterval:0.033 target:self
    selector:@selector(gameLoop:) userInfo:nil repeats:NO];
   }

This code is from a iPhone game development book. I don't know why the gameLoop method need to call the NSTimer again? in the applicationDidFinishLaunching, it set the NSTimer to do, why don't let it do every 0.033s, why add the same NSTimer code in the gameLoop method? thz.

Update: Sorry, my question should be clarify... ...Why the code don't just simply call repeats: YES?

A: 

Because the NSTimer is created with "repeats:NO" it fires once and then invalidates itself automatically, thereby preventing the timer from firing again.

So, you need a new timer every time when using "repeats:NO".

David Gelhar
A: 

Is this a joke? set repeats:YES. You specifically told it NOT to keep firing. So:

    - (void) applicationDidFinishLaunching:(UIApplication *)application
   {
    //set up main loop 
    [NSTimer scheduledTimerWithTimeInterval:0.033
    target:self selector:@selector(gameLoop:) userInfo:nil repeats:YES];
    //create instance of the first GameState 
    [self doStateChange:[gsMain class]];
   }

This is even self documented!!

Jason Coco
Why don't change it to YES?
Tattat
David Gelhar makes a good point as to why but your question was kind of unclear before the edit (which is when i answered).
Jason Coco
I just come across his comment, thz u any way.
Tattat
A: 

You could certainly get away with turning repeats:YES on:

    //set up main loop 
[NSTimer scheduledTimerWithTimeInterval:0.033
target:self selector:@selector(gameLoop:) userInfo:nil repeats:YES];

Actually, you would be better off this way, because the timing would be more consistent - the code you have listed here only starts the new timer after the frame has been calculated, which will give you irregular frames if those calls take varying amounts of time.

jexe
Or maybe the intention is to have the delay calculation start from the point the view finishes rendering?? If so, the original author certainly should have noted this is a comment, because it's pretty baffling otherwise why he isn't just using repeats:YES
David Gelhar