我目前在 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/