- (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];
}
此代碼來自iPhone遊戲開發書。我不知道爲什麼gameLoop方法需要再次調用NSTimer?在applicationDidFinishLaunching中,它設置NSTimer來做,爲什麼不讓它每0.033s做一次,爲什麼要在gameLoop方法中添加相同的NSTimer代碼?太赫茲。爲什麼需要在此代碼中再次調用NSTimer,而不是重複:YES?
更新:對不起,我的問題應該澄清......爲什麼代碼不只是簡單地調用重複:YES?
可能是因爲它的代碼錯誤。在這種情況下,保持對計時器的引用並在你不希望它再次觸發時使其自動失效更有意義。 – 2010-03-28 16:13:09