SKLabelNode delays app start

2019-02-17 09:13发布

at the moment I'm coding a little app with SpriteKit, which works perfectly fine, but the only problem is an SKLabelNode, which I initialize with the following normal piece of code:

​self.scoreLabel = [SKLabelNode labelNodeWithFontNamed:@"Futura"];
self.scoreLabel.fontSize = 190.0f;
self.scoreLabel.color = [SKColor whiteColor];
self.scoreLabel.alpha = .2;
self.scoreLabel.text = @"00";
self.scoreLabel.position = CGPointMake(screenWidth/2,self.scoreLabel.frame.size.height/2);
[self addChild:self.scoreLabel];

There a many more things to initialize, but they doesn't affect anything. If I comment out the code above, the app loads in the usual time. With the SKLabelNode it delays the loading by few seconds...

Hope anyone can help me.

3条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-02-17 10:00

You need to preload the font so that when you first call your SKScene sprite kit already has the font ready in memory. The best way to do this is to either use the app delegate or a singleton as soon as you can in your games life cycle.

self.scoreLabel = [SKLabelNode labelNodeWithFontNamed:@"Futura"];
self.scoreLabel.text = @"preload";

You don't need to make the label global, just make sure you keep a strong reference to let the compiler know you want it to keep the font in memory. Also make sure you set the text property (the font is only loaded when you set this).

EDIT:

This is what I am using:

- (void)setupFonts {
    SKLabelNode *preloadFontLabel = [SKLabelNode labelNodeWithFontNamed:@"HelveticaNeue-UltraLight"];
    [preloadFontLabel setText:@"Preload"];
}

This is called from a game assets singleton right when the game starts, I also have a call-back that stops the "PLAY" button appearing before the assets have loaded so the user can't jump the gun until everything is up and loaded. My game assets only take 1.03secs to load so its not an issue, but you could also hide a longer load behind a splash/startup screen if needed.

If your still seeing a problem I would check you don't have any other fonts in your SKScene that your not preloading. Also it could be an issue with loading a custom font, try changing the font to "HelveticaNeue-UltraLight" and see if you get the same issue.

查看更多
三岁会撩人
3楼-- · 2019-02-17 10:04

Be careful you aren't loading the entire font family. If I load "Chalkboard SE" it will take 4-6 seconds, and appear to work.

But if I load ChalkboardSE-Regular , it's virtually instantaneous ~100ms or less.

If you want to know more, I've got various links and timings on my humble blog. https://gilesey.wordpress.com/2015/01/14/ios-spritekit-font-loading-times-of-sklabelnodes/

查看更多
看我几分像从前
4楼-- · 2019-02-17 10:05

Actually, you don't need to preload the font for an SKLabelNode. The delay is caused by the fact that you're using the wrong font name. There's no "Futura" font on iOS - what you probably mean is "Futura-Medium". Replace "Futura" with "Futura-Medium", and you should see the load time drastically fall.

(You can still preload your fonts, but it's not necessary; lazy loading is pretty fast provided you use a correct font name.)

查看更多
登录 后发表回答