我应该使用什么方法预加载精灵?

max*_*hud 1 ios sprite-kit skspritenode

我知道你可以通过在游戏开始时创建它们来预加载spritenodes,但有没有更有效的方法呢?

Dob*_*pir 5

有关预加载纹理的信息,请参阅SKTexture部分.

创建SKTexture而不是SKSpriteNode.然后你可以使用ether preloadWithCompletionHandler:或类方法preloadTextures:withCompletionHandler:

处理预加载的一种好方法是使用纹理图集.通过使用纹理图集,您可以将所有纹理组合成一个包含所有纹理的较大"图纸".这允许Sprite Kit仅创建一个纹理对象以加载到VRAM中.由于它只是显示精灵表的一部分,因此每次通过GPU时,地图集中的所有纹理都可以绘制到屏幕上.

这是我使用preloadWithCompletionHandler:方法预加载纹理的方法:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{

//  Preload the textures
self.artAtlas = [SKTextureAtlas atlasNamed:@"art"];
[self.artAtlas preloadWithCompletionHandler:^{
//  Textures loaded so we are good to go!

    /* Pick a size for the scene
     Start with the current main display size.
     */
    NSInteger width = [[NSScreen mainScreen] frame].size.width;
    NSInteger height = [[NSScreen mainScreen] frame].size.height;
    JRWTitleScene *scene = [JRWTitleScene sceneWithSize:CGSizeMake(width, height)];

    /* Set the scale mode to scale to fit the window */
    scene.scaleMode = SKSceneScaleModeAspectFit;

    [self.skView presentScene:scene];
}];

#if DEBUG
self.skView.showsFPS = YES;
self.skView.showsNodeCount = YES;
#endif

}
Run Code Online (Sandbox Code Playgroud)

有关预加载纹理的更多信息,请参阅Sprite Kit编程指南的相关部分.