Im, пытающийся создать цикл игры NSThread, я некоторое время смог получить успешный 57 FPS.Создание игровой петли NSThread
Некоторое время, когда мои fps поднимаются до некоторого числа смешных.
Я не понимаю, как это происходит.
Я проверяю, сколько времени прошло с последнего цикла, и если бы оно было быстрым, спите поток столько времени.
Это не всегда происходит, это иногда ускользает от проверки скорости и делает цикл быстрым.
Любые комментарии означали бы много.
И где я подходила к 'Tick'?
- (void)gameLoop{
//gameIsRunning is set to TRUE in viewDidLoad
while (gameIsRunnning){
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
//Get Current date
NSDate *curTime = [NSDate date];
//Time since last loop and vurrent date;
NSTimeInterval timePassed_ms = [curTime timeIntervalSinceDate:old_date];// * 1000.0;
NSLog(@"***************");
//Cout the time interval
NSLog(@"Loop Time %f",timePassed_ms);
//Check if the loop was to fast and sleep for long enough to make up for about 60 FPS
if (timePassed_ms < 1.0/60) {
double timeToSleep = timePassed_ms - (1.0/60);
timeToSleep = timeToSleep*-1;
NSLog(@"Sleep For %f",timeToSleep);
[NSThread sleepForTimeInterval:timeToSleep];
}
//This new date is to try and check if after waiting the loop is taking the correct duration
NSDate *newDate = [NSDate date];
NSTimeInterval timePassed_after = [newDate timeIntervalSinceDate:curTime];// * 1000.0;
//Make an fps out of this new time interval after wait
double FPS = (1.0/timePassed_after);
NSLog(@"FPS %f",FPS);
NSLog(@"Adjusted Time %f",timePassed_after);
NSLog(@"***************");
//Reset olddate for next loop
old_date = curTime;
//Apparently this will capture touches and button events
while(CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.002, TRUE) == kCFRunLoopRunHandledSource);
//A test on moving a ball to see how smooth it will be
[self performSelectorOnMainThread:@selector(moveBall) withObject:nil waitUntilDone:NO];
[pool drain];
}
}
Другие темы или даже процессы на вашем компьютере может влияют на время этого потока. Что еще происходит в вашем приложении? – user1118321
Только метод перемещает мяч. – Necro