【问题标题】:Loading screen to game transition加载屏幕到游戏过渡
【发布时间】:2012-10-29 14:58:24
【问题描述】:

当我启动我的游戏时,我希望屏幕在后台加载资源时显示加载图像。为此,我加载了一个简单的 UIImageView 组件并向其添加“微调器”,以向用户提供设备正在后台加载某些内容的反馈。

在显示此图像时,我会加载所有图像和纹理并设置我的 OpenGL 视图并让它渲染。 当第二帧被渲染时,我想隐藏 ImageView 并且只显示 OpenGL 视图。我不希望 OpenGL 视图显示在第一帧上,因为渲染需要很长时间。

但是,我在加载所有资源并为 OpenGL 视图设置 DisplayLink 以在新线程中进入渲染循环,然后在加载完成时显示 OpenGL 视图时遇到了一些问题。渲染循环似乎没有开始。

这是我的 View Controller 的 loadView 方法

- (void)loadView 
{
CGRect mainScreenFrame = [[UIScreen mainScreen] applicationFrame];

    // Set up the image view
    UIImage *img = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"Startscreen" ofType:@"png"]];
    _imageView = [[UIImageView alloc] initWithFrame:mainScreenFrame];
    _imageView.image = img;

    // Set up the spinner
    _spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    [_spinner setCenter:CGPointMake(mainScreenFrame.size.height/3.0*1.85, mainScreenFrame.size.width/10.0*7.55)];
    [_imageView addSubview:_spinner];    
    [_spinner startAnimating];

    // Show the loading image
    self.view = _imageView;

    /* Load resources in a new thread -- this shows the spinner during loading */
    [NSThread detachNewThreadSelector:@selector(loadGLView) toTarget:self withObject:nil];
}

loadGLView 只做以下事情,初始化 OpenGL View 并开始加载过程。

_glView = [[JungleOpenGLView alloc] initWithFrame:mainScreenFrame];

这就是我在 OpenGL 视图中设置 DisplayLink 的方式。

CADisplayLink* displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(setupRender:)];
[displayLink setFrameInterval:2];
[displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];

当第二帧被渲染时,我发送一个通知,然后 ViewController 设置

self.view = _glView;

【问题讨论】:

    标签: iphone ios opengl-es opengl-es-2.0


    【解决方案1】:

    我发现了我的错误。 而不是像这样为加载方法创建一个新线程:

    [NSThread detachNewThreadSelector:@selector(loadGLView) toTarget:self withObject:nil];
    

    我用过:

    [self performSelectorInBackground:@selector(loadGLView) withObject:nil];
    

    这就是诀窍。关于这个主题(创建线程)的好读物可以在这里找到:https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Multithreading/CreatingThreads/CreatingThreads.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多