Delay when calling SKLabelNode?

Viewed 1145

I am having a problem with a slight delay (lag) when transitioning from one SKScene to another. By commenting out various bit of code I have narrowed this down to SKLabelNode, my guess is thats its loading / caching the font when called which is resulting in a small delay/stutter when stating up the new SKScene.

Has anyone else noticed this, its less obvious when your just using a single SKScene (like the default template) as the slowdown just gets lost in the usual startup delay. Does anyone know a way round this, is there a way to pre-load the font? I guess I could load the font on the UIViewController at startup and see if I could access it from with the SKScene, anyone any ideas?

-(id)initWithSize:(CGSize)size {
    if (self = [super initWithSize:size]) {
        [self setScore:0];

        [self setBackgroundColor:[SKColor blackColor]];
        SKLabelNode *labelNode = [SKLabelNode labelNodeWithFontNamed:@"System"];
        [labelNode setText:@"00000"];
        [labelNode setFontSize:20.0];
        [labelNode setPosition:CGPointMake(CGRectGetMidX(self.frame),500)];
        [labelNode setName:@"SCORE"];
        [labelNode setAlpha:1.0];
        [self addChild:labelNode];
        [self setScoreLabel:labelNode];
        ...
6 Answers

The delay is based on the loading of your font. Best to preload fonts, sounds, and any other assets you intend to use, so that you don't have a delay when it's actually used the first time.

You can preload in your setup with :

SKLabelNode *preload = [SKLabelNode labelNodeWithFontNamed:@"System"];
[preload setText:@"anything"]; 

As noted in the comments, preloading is only needed when using a font that is not available via iOS.

The usual pre-load trick is to create a "dummy" version of the asset in your app delegate, which should effectively cache the custom font in your case at runtime. This will also help pinpoint if this is the real issue or not - there are many ways that stutters are introduced that are tough to efficiently track down in Sprite Kit.

I had a delay in SKScene rendering because I was using multiple UIViewControllers to navigate through my app rather than having a single UIViewController whose view is an SKView; found the answer here. As soon as I refactored my code to use a single UIViewController transitioning between SKScenes became seamless.

If this is not your issue, maybe you could instantiate your SKScenes when your view loads and do any update to the scene's content just before you transition and present the scene.

Related