什么是更好的方式在iPhone上创建一个游戏循环,而不是使用NSTimer? [英] What is a better way to create a game loop on the iPhone other than using NSTimer?

查看:97
本文介绍了什么是更好的方式在iPhone上创建一个游戏循环,而不是使用NSTimer?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在iPhone上编程游戏。我目前使用NSTimer触发我的游戏更新/渲染。这个问题是(在性能分析后)我似乎在更新/渲染之间丢失了很多时间,这似乎主要是与我插入NSTimer的时间间隔。

I am programming a game on the iPhone. I am currently using NSTimer to trigger my game update/render. The problem with this is that (after profiling) I appear to lose a lot of time between updates/renders and this seems to be mostly to do with the time interval that I plug into NSTimer.

所以我的问题是什么是使用NSTimer的最好的替代方法?

So my question is what is the best alternative to using NSTimer?

/ p>

One alternative per answer please.

推荐答案

你可以通过线程获得更好的性能,尝试这样:

You can get a better performance with threads, try something like this:

- (void) gameLoop
{
    while (running)
    {
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
        [self renderFrame];
        [pool release];
    }
}

- (void) startLoop
{
    running = YES;
#ifdef THREADED_ANIMATION
    [NSThread detachNewThreadSelector:@selector(gameLoop)
    toTarget:self withObject:nil];
#else
    timer = [NSTimer scheduledTimerWithTimeInterval:1.0f/60
    target:self selector:@selector(renderFrame) userInfo:nil repeats:YES];
#endif
}

- (void) stopLoop
{
    [timer invalidate];
    running = NO;
}

renderFrame 方法准备帧缓冲区,绘制帧并在屏幕上呈现帧缓冲区。 (PS有关各种类型的游戏循环及其优点和缺点的伟大的文章。)

In the renderFrame method You prepare the framebuffer, draw frame and present the framebuffer on screen. (P.S. There is a great article on various types of game loops and their pros and cons.)

这篇关于什么是更好的方式在iPhone上创建一个游戏循环,而不是使用NSTimer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆