Delay when calling SKLabelNode?

我怕爱的太早我们不能终老 提交于 2019-12-21 18:04:43

问题


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

回答1:


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.




回答2:


One thing I've noticed is that if you don't use a "real" font name when you create a SKLabelNode, you get the delay you mentioned. Since "System" is not an actual font name, you're experiencing the delay. Try choosing one of the actual fonts as listed by the system. (For example, try "HelveticaNeue" instead of "System".)




回答3:


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.




回答4:


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.




回答5:


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.

Here's a list of the actual font names you should use: AcademyEngravedLetPlain

AlNile-Bold

AlNile

AmericanTypewriter-Light

AmericanTypewriter-CondensedLight

AmericanTypewriter-CondensedBold

AmericanTypewriter

AmericanTypewriter-Condensed

AmericanTypewriter-Bold

AppleColorEmoji

AppleSDGothicNeo-Thin

AppleSDGothicNeo-UltraLight

AppleSDGothicNeo-SemiBold

AppleSDGothicNeo-Medium

AppleSDGothicNeo-Regular

AppleSDGothicNeo-Bold

AppleSDGothicNeo-Light

ArialMT

Arial-BoldItalicMT

Arial-ItalicMT

Arial-BoldMT

ArialHebrew-Bold

ArialHebrew-Light

ArialHebrew

ArialRoundedMTBold

Avenir-Heavy

Avenir-Oblique

Avenir-Black

Avenir-Book

Avenir-BlackOblique

Avenir-HeavyOblique

Avenir-Light

Avenir-MediumOblique

Avenir-Medium

Avenir-LightOblique

Avenir-Roman

Avenir-BookOblique

AvenirNext-MediumItalic

AvenirNext-Bold

AvenirNext-UltraLight

AvenirNext-DemiBold

AvenirNext-HeavyItalic

AvenirNext-Heavy

AvenirNext-Medium

AvenirNext-Italic

AvenirNext-UltraLightItalic

AvenirNext-BoldItalic

AvenirNext-Regular

AvenirNext-DemiBoldItalic

AvenirNextCondensed-Regular

AvenirNextCondensed-MediumItalic

AvenirNextCondensed-UltraLightItalic

AvenirNextCondensed-UltraLight

AvenirNextCondensed-BoldItalic

AvenirNextCondensed-Italic

AvenirNextCondensed-Medium

AvenirNextCondensed-HeavyItalic

AvenirNextCondensed-Heavy

AvenirNextCondensed-DemiBoldItalic

AvenirNextCondensed-DemiBold

AvenirNextCondensed-Bold

BanglaSangamMN

BanglaSangamMN-Bold

Baskerville-Bold

Baskerville-SemiBoldItalic

Baskerville-BoldItalic

Baskerville

Baskerville-SemiBold

Baskerville-Italic

BodoniSvtyTwoITCTT-Book

BodoniSvtyTwoITCTT-Bold

BodoniSvtyTwoITCTT-BookIta

BodoniSvtyTwoOSITCTT-BookIt

BodoniSvtyTwoOSITCTT-Bold

BodoniSvtyTwoOSITCTT-Book

BodoniSvtyTwoSCITCTT-Book

BodoniOrnamentsITCTT

BradleyHandITCTT-Bold

ChalkboardSE-Light

ChalkboardSE-Regular

ChalkboardSE-Bold

Chalkduster

Cochin-Bold

Cochin-BoldItalic

Cochin-Italic

Cochin

Copperplate

Copperplate-Light

Copperplate-Bold

Courier

Courier-Oblique

Courier-BoldOblique

Courier-Bold

CourierNewPSMT

CourierNewPS-BoldMT

CourierNewPS-ItalicMT

CourierNewPS-BoldItalicMT

DamascusBold

Damascus

DamascusLight

DamascusMedium

DamascusSemiBold

DevanagariSangamMN

DevanagariSangamMN-Bold

Didot-Bold

Didot-Italic

Didot

DINAlternate-Bold

DINCondensed-Bold

EuphemiaUCAS

EuphemiaUCAS-Bold

EuphemiaUCAS-Italic

Farah

Futura-Medium

Futura-CondensedMedium

Futura-MediumItalic

Futura-CondensedExtraBold

GeezaPro-Bold

GeezaPro

Georgia-BoldItalic

Georgia-Bold

Georgia-Italic

Georgia

GillSans

GillSans-Italic

GillSans-BoldItalic

GillSans-Light

GillSans-LightItalic

GillSans-Bold

GujaratiSangamMN-Bold

GujaratiSangamMN

GurmukhiMN-Bold

GurmukhiMN

STHeitiSC-Medium

STHeitiSC-Light

STHeitiTC-Medium

STHeitiTC-Light

Helvetica-Oblique

Helvetica-Light

Helvetica-Bold

Helvetica

Helvetica-BoldOblique

Helvetica-LightOblique

HelveticaNeue-BoldItalic

HelveticaNeue-Light

HelveticaNeue-Italic

HelveticaNeue-UltraLightItalic

HelveticaNeue-CondensedBold

HelveticaNeue-MediumItalic

HelveticaNeue-Thin

HelveticaNeue-Medium

HelveticaNeue-ThinItalic

HelveticaNeue-LightItalic

HelveticaNeue-UltraLight

HelveticaNeue-Bold

HelveticaNeue

HelveticaNeue-CondensedBlack

HiraKakuProN-W6

HiraKakuProN-W3

HiraMinProN-W6

HiraMinProN-W3

HoeflerText-Regular

HoeflerText-BlackItalic

HoeflerText-Italic

HoeflerText-Black

IowanOldStyle-Bold

IowanOldStyle-BoldItalic

IowanOldStyle-Italic

IowanOldStyle-Roman

Kailasa

Kailasa-Bold

KannadaSangamMN

KannadaSangamMN-Bold

KhmerSangamMN

KohinoorDevanagari-Light

KohinoorDevanagari-Book

KohinoorDevanagari-Medium

LaoSangamMN

MalayalamSangamMN

MalayalamSangamMN-Bold

Marion-Regular

Marion-Italic

Marion-Bold

MarkerFelt-Thin

MarkerFelt-Wide

Menlo-BoldItalic

Menlo-Regular

Menlo-Bold

Menlo-Italic

DiwanMishafi

Noteworthy-Bold

Noteworthy-Light

Optima-Regular

Optima-Italic

Optima-Bold

Optima-BoldItalic

Optima-ExtraBlack

OriyaSangamMN

OriyaSangamMN-Bold

Palatino-Roman

Palatino-Italic

Palatino-Bold

Palatino-BoldItalic

Papyrus-Condensed

Papyrus

PartyLetPlain

SavoyeLetPlain

SinhalaSangamMN

SinhalaSangamMN-Bold

SnellRoundhand-Black

SnellRoundhand-Bold

SnellRoundhand

Superclarendon-Regular

Superclarendon-BoldItalic

Superclarendon-Light

Superclarendon-BlackItalic

Superclarendon-Italic

Superclarendon-LightItalic

Superclarendon-Bold

Superclarendon-Black

Symbol

TamilSangamMN

TamilSangamMN-Bold

TeluguSangamMN

TeluguSangamMN-Bold

Thonburi-Bold

Thonburi

Thonburi-Light

TimesNewRomanPS-BoldItalicMT

TimesNewRomanPSMT

TimesNewRomanPS-BoldMT

TimesNewRomanPS-ItalicMT

Trebuchet-BoldItalic

TrebuchetMS

TrebuchetMS-Bold

TrebuchetMS-Italic

Verdana-BoldItalic

Verdana-Italic

Verdana

Verdana-Bold

ZapfDingbatsITC

Zapfino

See https://gilesey.wordpress.com/2015/01/14/ios-spritekit-font-loading-times-of-sklabelnodes/




回答6:


I added this method in my "AppDelegate.m":

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

and called it in:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [self setupFonts];
    return YES;
}

The app didn't load any slower and it worked in the whole app flawlessly. Found the answer here: SKLabelNode delays app start



来源:https://stackoverflow.com/questions/20380954/delay-when-calling-sklabelnode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!