5

I'm making a Sprite Kit app and in my scene I added an SKLabelNode. I noticed a pretty large lag-spike when I load the SKScene. After profiling the app I found it came from creating an SKLabelNode with a papyrus font(though the font doesn't matter). When I remove the label the scene starts up almost instantaneously, but with the label it takes an extra 1-3 seconds.

I am pretty sure it's from loading the font as when I go back to the main menu and play the game again it starts up instantly again.

Now is there a way to preload the font earlier so when the player selects the level there isn't a large pause?

Exascale
  • 929
  • 1
  • 7
  • 17

2 Answers2

14

We had this issue, and it turned out that we were simply not using the "correct" font name. In our case, we were using "Menlo" instead of "Menlo-Regular" when instantiating our SKLabelNode, and it was incurring the several-second penalty. Once we used the correct font name, the delay no longer happened.

(Curiously, the SKLabelNode still found Menlo and used it, so it was not immediately obvious that we had the wrong font name. Presumably, the delay is caused by the system having to figure out the appropriate substitute to use. It does a good job, finding the font we intended to use, but it takes a while to do it, hence the delay.)

cc.
  • 3,041
  • 1
  • 21
  • 24
  • Nice find, will have to check this. – Dvole May 13 '14 at 13:58
  • Just had this issue and this turned out to be the culprit, wish it would just throw an error or default to the system font when this happens. – Devin Aug 29 '14 at 18:45
  • 1
    Thanks, this was exactly my problem. Ran the app with profiler and noticed that it spends 95% of it's loading time setting `fontName` property on the label. Changed from "Marion" to "Marion-Regular" and problem is gone. – i4niac Sep 01 '14 at 10:31
3

I had the same issue. Add following code to your very first scene, with your font name:

SKLabelNode *preload = [SKLabelNode labelNodeWithFontNamed:@"Avenir"];
preload.text = @"text";

If you don't provide text it won't load font. Note that you don't need to add label as child to your scene.

Dvole
  • 5,725
  • 10
  • 54
  • 87
  • It's been a while since I've played with that project, but upon seeing this answer I tinkered with it and added that code. It makes everything much faster. – Exascale Mar 29 '14 at 10:34
  • Great, but no way to be notified "on completion"? (e.g., a block)... I use Avenir for my UI, but Menlo (monospaced) for the score counter. The title scene loads pretty quick, but the first time I transition to the game scene (load second font), it takes 2 seconds or more to react... – Nicolas Miari Apr 06 '14 at 15:21
  • Oh, wait... guess that `-labelFontWithfontNamed:` stalls (doesn't return right away) until complete, so calling it on launch (before showing view/scenes etc.) should do the trick? – Nicolas Miari Apr 06 '14 at 15:22
  • 1
    Note that the above code is *probably* unnecessary. The font name is incorrect - the correct font name is "Avenir-Medium" (or perhaps you wanted "Avenir-Roman"). Use the correct name, and the SKLabelNode will not incur the several-second startup time. – cc. May 13 '14 at 13:45
  • Ensuring I set a text value made the difference for me. – roycable Jun 03 '16 at 06:37