1

我们正在开发一款包含大量图像资源的游戏,我们希望将应用程序大小保持在 30MB 以下,因此决定下载 zip 文件中的图像。第一个场景有 50 个 SKSpriteNode 对象和 426 个用于纹理和动画的图像,我们无法创建纹理图集,因为我们的图像不在捆绑包中。从 iOS8 开始,我们[SKTextureAtlas atlasWithDictionary:]可以从不在捆绑资源中的图像编译 atlasc 文件,但我们也必须支持 iOS7。[UIImage imageWithContentsOfFile:imagePath]当我们使用和方法加载 426 张图像时,[SKTexture textureWithImage:textureImage]我们的应用程序由于内存使用量大而关闭。

代码:

for (int i=0; i <= numImages-1; i++) {
    NSString *textureName = [NSString stringWithFormat:@"texture_%05d", i];
    SKTexture *texture = [SKTexture textureWithImage:ImagePath(textureName)];
    [frames addObject:texture];
}

之后:

SKAction *action = [SKAction animateWithTextures:frames timePerFrame:kAnimationFrameTime resize:YES restore:YES];
[node runAction:action withKey:kAnimationActionKey];

ImagePath 宏返回 Documents 目录中给定图像的绝对路径。

是否有从 SpriteKit 的 Documents 目录加载大量动画图像的最佳实践?

4

0 回答 0