OGeek|极客世界-中国程序员成长平台

标题: ios - SpriteKit 有时无法加载 SKTexture [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 13:16
标题: ios - SpriteKit 有时无法加载 SKTexture

我目前在 SpriteKit 和 SKTexture 预加载方面遇到了一个奇怪的间歇性问题。我有多个图像数组,我按以下格式预加载:

- (NSDictionary *)loadTexturesWithNamesNSArray *)aNames
{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    NSMutableArray *textureArray = [NSMutableArray array];

    for (NSString *textureName in aNames)
    {
        SKTexture *texture = [SKTexture textureWithImageNamed:textureName];
        if (texture)
        {
            [textureArray addObject:texture];
            [dict setObject:texture forKey:textureName];
        }
    }

    [SKTexture preloadTextures:textureArray withCompletionHandler:^{
        _iCounter++;
        [self loadTextures];
    }];

    return dict;
}

这在 90% 的情况下都可以正常工作,但偶尔会出现以下错误:

SKTexture: Error loading image resource:

这不会发生在任何特定的图像集上,只是随机发生。因此,有时播放器会加载正常,而其他播放器则不会。这可能是内存问题吗?我使用 _iCounter 拆分纹理加载,确保在开始下一组图像之前加载前一组图像,以尝试停止过多的并发加载。这加快了进程,但我仍然看到这个间歇性问题。

有没有其他人看到过这种情况,或者知道是什么原因造成的?



Best Answer-推荐答案


根据我的经验,一个常见的原因是您没有等待调用完成处理程序。例如,在“图像”是预加载的图像之一的情况下执行此类操作可能是错误的来源:

[self loadTexturesWithNames:names];
SKSpriteNode* sprite = [SKSpriteNode spriteNodeWithImageNamed"image"];

当然,如果你只在 loadTexture 方法中创建 Sprite 节点和纹理应该没问题。除非您执行其他后台任务或在后台运行 loadTextureWithNames 选择器(单独的线程)。

关于ios - SpriteKit 有时无法加载 SKTexture,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27292575/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4