0

I'm setting a label

let scoreLabel = SKLabelNode(fontNamed: "Edit Undo Line BRK")

in a function

func increaseScoreBy(increment: Int) {
    score += increment
    scoreLabel.text = "Score: 100"
    scoreLabel.removeAllActions()
    scoreLabel.runAction(scoreFlashAction)
}

and if I comment out the scoreLabel.text = "Score: 100" then it will run perfectly but the score obviously won't update. originally I had it \(score) but I changed it to static text to see if that was the issue. any ideas on why this would act like this?

there is one thing, if its \(score) instead of 100 it will pause (temporarily freeze) the game every time the score updates, if its hard coded, it only freezes the first time.

update

Initialization of the scoreLabel in the game setup

scoreLabel.fontsize = 50
scoreLabel.text = "Score: 0"
scoreLabel.name = "scoreLabel"
scoreLabel.verticalAlignmentMode = .Center
scoreLabel.position = CGPoint( x: size.width / 2, y: size.height - scoreLabel.frame.size.height + 3)
hudLayerNode.addChild(scoreLabel)

scoreFlashAction = SKAction.sequence([SKAction.scaleTo(1.5, duration: 0.1), SKAction.scleTo(1.0, duration: 0.1)])
scoreLabel.runAction(SKAction.repeatAction(scoreFlashAction, count: 20))

update

It is definitely the font type some how. I removed the fontName and just made a regular label and no issues, but of course the font is wrong.

Jason G
  • 2,395
  • 2
  • 24
  • 34

2 Answers2

0

Your problem is actually the font and the .text. It looks like the font get loaded if needed. So you need to set a font-variable to save your font, which you can access later, whenever you want.

let font = UIFont(name: "Edit Undo Line BRK", size: yourSize)

I would recommend you to set it at application start and save it inside a singleton for example. Then you can access the font like that:

let scoreLabel = SKLabelNode(fontNamed: font?.fontName)

It should take much less time now.

Christian
  • 22,585
  • 9
  • 80
  • 106
  • I tried doing this and it kept telling me that gameScene didn't have a type font. I'm pretty new to swift, so I'm not sure what is going on. – Jason G Feb 18 '15 at 13:58
  • it still says GameScene.Type does not have a member named 'font', I put let font = UIFont(name: "Edit Undo Line BRK", size: 50) in the declarations – Jason G Feb 18 '15 at 15:43
0

some one deleted the answer so here it is again

Thanks to

How to cache or preload SKLabelNode font?

for a hint at what was going on. While the name was correct, I had not "added" the font to the app. to do this click on your targets and the info tab. create a new key of "Fonts provided by application" under the customer ios target properties. expand it and add the font name as the value of the item. don't forget to copy the ttf into your application folders and make sure copy and target are both selected when you do.

Community
  • 1
  • 1
Jason G
  • 2,395
  • 2
  • 24
  • 34