如何在Sprite Kit中预加载纹理?

3

我有一些动画帧在我的节点上,第一次播放动画时会出现卡顿和fps下降。之后的每一次都没问题。

如何预加载纹理以使其流畅运行?

我有这个方法在加载游戏时运行:

- (void)load
{
    self.animationFrames = @[[SKTexture textureWithImageNamed:@"exp1"], [SKTexture textureWithImageNamed:@"exp2"],
                             [SKTexture textureWithImageNamed:@"exp3"], [SKTexture textureWithImageNamed:@"exp4"], [SKTexture textureWithImageNamed:@"exp5"], [SKTexture textureWithImageNamed:@"exp6"], [SKTexture textureWithImageNamed:@"exp7"], [SKTexture textureWithImageNamed:@"exp8"], [SKTexture textureWithImageNamed:@"exp9"]];
}

而这种播放动画的方法:

-(void)playExplosionAnimation
{
    self.size = CGSizeMake(250, 250);
    SKAction *animation = [SKAction animateWithTextures:self.animationFrames timePerFrame:0.1];

    [self runAction:animation completion:^{
        self.hidden = YES;
    }];
}
1个回答

10

你应该创建一个纹理集合并使用SKTextureAtlas方法:

 preloadWithCompletionHandler:
+ preloadTextureAtlases:withCompletionHandler:

以下是文档对此的说明:

Sprite Kit创建了一个后台任务,用于从图集加载纹理数据。然后,Sprite Kit将控制权返回给您的游戏。在加载纹理图集之后,将调用您的完成处理程序。

如果您需要同时预加载多个纹理图集,请改用preloadTextureAtlases:withCompletionHandler:方法。


1
我在预加载纹理图集时遇到了fps下降的问题,尽管我已经在Instruments中验证了这项工作是在工作线程上完成的。我还尝试将其包装在全局调度队列的“async”块中,但没有帮助。你有什么想法是什么原因导致这个问题吗? - damirstuhec

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接