问题
I am new to iPhone development. I used [NSTimer scheduledTimerWithTimeInterval:0.01]
for game loop. The game consists drawscreen function in which I use CGContextClipToRect
to clip the large images for animation.
But the speed 0.01 seconds is working in simulator only not on device. How can I overcome this problem?
The timer code is in view controller as
(void)viewDidLoad {
GameView *main = [[GameView alloc]
initWithFrame:[[UIScreen mainScreen] applicationFrame]];
main.backgroundColor = [UIColor blackColor];
self.view = main;
[main release];
self.tim = [NSTimer scheduledTimerWithTimeInterval: 0.01
target: self selector: @selector (gameloop:) userInfo: nil repeats: YES];
[super viewDidLoad];
}
Anyone can help me?
回答1:
NSTimer is the wrong tool for this job. It's not meant to be a real-time timer. It has no guarantees on when it will fire, and you can miss frames easily.
There are a lot of good recommendations for how to develop this kind of program on this thread. Note particularly the references to Apple's sample code.
回答2:
As U62 said, 100fps is not a realistic expectation, try running it at 30, maybe 60 updates a second and see how it runs. Also if your writing a game that need to run at a high frame rate you should look into writing it in OpenGL.
One option may be to process logic every tick, but only draw to the screen every few ticks.
回答3:
i mean ,for example when i animate frames ,frames are shown in correct speed in simulator. but in device it shows very slow(FPS)in other words a boy runs fastly in simulator.but in device he walks.
来源:https://stackoverflow.com/questions/915410/nstimer-on-device-is-slower-than-in-simulator