从更新调用时,GLKTextureLoader失败

Dav*_*ohn 6 xcode opengl-es ios

从viewDidLoad加载纹理工作正常.但是,如果我尝试从GLKViewController更新中加载它们,我会收到错误.我这样做是因为我想在不改变视图的情况下交换新的背景纹理.

这在上次升级之前有效.也许我对时间很幸运.我怀疑它是失败的,因为某些线程正忙或什么?

这是完整的错误.

Domain = GLKTextureLoaderErrorDomain Code = 8"无法完成操作.(GLKTextureLoaderErrorDomain error 8.)"UserInfo = 0x10b5b510 {GLKTextureLoaderGLErrorKey = 1282,GLKTextureLoaderErrorKey = OpenGL error}

所以问题是,我可以安全地从GLKViewController更新函数加载纹理吗?或者我是否需要重新考虑我的方法并重新加载整个视图或其他内容?

这是我的功能:

-(void) LoadTexture:(NSString *)texture textureInfo:(GLKTextureInfo**)textureInfo
{
    NSString *path = [[NSBundle mainBundle] pathForResource:texture ofType:@"png"]; 
    NSError *error = nil;

    (*textureInfo) = [GLKTextureLoader textureWithContentsOfFile:path options:nil error:&error];

    NSLog(@"path %@", path);

    if(!(*textureInfo))
    {
        NSLog(@"Failed to load texture %@ %@", texture, error);  
    }
    else
    {
        NSLog(@"LOADED Texture %@ !!! YAY!!! ", texture);
    }
}
Run Code Online (Sandbox Code Playgroud)

谢谢,

大卫

小智 0

我有和你类似的问题。我为解决这个问题所做的就是创建一个类,其中包含我想要在整个游戏中使用的所有纹理。我viewDidLoad:初始化了该类并加载了所有纹理。当我需要使用任何纹理时,它们已经加载并且问题没有发生。

例如。在viewDidLoad

GameTextures *textures = [GameTextures alloc] init];
[textures LoadAll];
Run Code Online (Sandbox Code Playgroud)

LoadAll 将加载所有纹理以供以后使用

然后当你需要使用纹理时

[myBackground setTexture: textures.backgroundTexture2];
Run Code Online (Sandbox Code Playgroud)

希望这有帮助:)