Search code examples
iossprite-kitskscenesklabelnode

Delay when calling SKLabelNode?


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];
        ...

Solution

  • 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.