ios - SpriteKit 有时无法加载 SKTexture

标签 ios sprite-kit preloading sktexture

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

- (NSDictionary *)loadTexturesWithNames:(NSArray *)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 分割纹理加载,确保前一组图像在开始下一组图像之前加载,以尝试停止过多的并发加载。这加快了进程,但我仍然看到这个间歇性问题。

有其他人看到过这个,或者知道什么可能导致这个吗?

最佳答案

根据我的经验,一个常见原因是您没有等待调用完成处理程序。例如,执行类似的操作,其中“图像”是预加载的图像之一,可能会出现错误:

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

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

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

相关文章:

css - 链接 rel ="preload"究竟是如何工作的?

jquery - 如何停止预加载?

iOS - genStrings -s 无法使用替换功能

iphone - 您如何在 iOS 中将模型与 UIButton(或通常的 UIViews)相关联?

ios - UITableViewCell 延迟实例化中的 `self.bounds.size.width`

ios - SpriteKit swift : How to prevent a Crash when bodyA collisions with more than one bodyB at the same time?

php - 在请求播放之前,如何阻止嵌入的声音片段下载到浏览器缓存中?

iphone - 如何在 iOS 上将图像旋转 90 度?

swift - 为什么我无法使用 SKPhysicsBody 构造函数?

ios - 如何在spritekit中录制游戏场景