1

I have a strange problem. I am using a label node in one of my projects. Last time i was adding a custom font and from that moment my label nodes are not working properly. If I set a Font to my label (like in example under this text), label just won't appear on screen, but app works ok. When clicking to button start game, it also takes a while to load game view ( I guess that font is loading?):

  label1 = [SKLabelNode labelNodeWithFontNamed:@"Arial"];

But if I don't set any Font to my label, App just crashes and i get a warning:

 *** Terminating app due to uncaught exception 'Attemped to add nil node', reason: 'Attemped to add nil node to parent: <SKScene> name:'(null)' frame:{{0, 0}, {568, 320}}'

I have read lots of posts here from people having problems with fonts. But I just can't find any similar.

Code where error appears (calling a function to stopwatch node to view):

-(void)addStopwatch
{

    stopwatch.position = CGPointMake(self.frame.size.width/16,self.frame.size.height);
    stopwatch = [SKLabelNode labelNodeWithFontNamed:@"Arial"];
    stopwatch.fontSize = 25;
    stopwatch.fontColor = [UIColor whiteColor];
    stopwatch.text = [NSString stringWithFormat:@"%s","00:00"];
    stopwatch.name = @"stopW";
    [self addChild:stopwatch];

 }

stopwatch sprite is declared in .h file:

SKLabelNode* stopwatch;
MOzeb
  • 423
  • 1
  • 6
  • 14
  • Does the error go away if you remove the custom font and clean the build folder (xcode: project -> clean)? Which labels are nil, the ones with the custom font or all others or just any label? Have you tried in a new SK project whether the custom font works in general? If even the new project fails, the font might be the issue. Otherwise you need to debug this more precisely (breakpoints, yada-yada). – CodeSmile Nov 04 '14 at 21:36
  • @LearnCocos2D I have tried put fonts that Xcode include. And not one label appears. I also tried to create a new SK label and it is also invisible when i run game. I have tried to delete custom font and clean project, but no luck.. – MOzeb Nov 04 '14 at 21:39
  • can you post the code (entire method or enough for context) where the error occurs? Add an exception breakpoint if you can't see it (Xcode shows main.m) – CodeSmile Nov 04 '14 at 21:42
  • @LearnCocos2D edited. Problem appears when i am calling a function to add stopwatch (which is label node) to view. – MOzeb Nov 04 '14 at 21:55
  • I have solved my problem with deleting the old method that was adding a sk label node and also deleting my label note from global initialization and recreating it with other name.. still don't know what happened. I think that implementing custom fonts was the problem. – MOzeb Nov 05 '14 at 19:14

2 Answers2

1
stopwatch.position = CGPointMake(self.frame.size.width/16,self.frame.size.height);
stopwatch = [SKLabelNode labelNodeWithFontNamed:@"Arial"];

The label is at position 0,0. Creating a label creates a new label with default properties. The old (if it ever existed) which had its position set is thrown away.

Try it this way:

stopwatch = [SKLabelNode labelNodeWithFontNamed:@"Arial"];
stopwatch.position = CGPointMake(self.frame.size.width/16,self.frame.size.height);

Also this is superfluous:

stopwatch.text = [NSString stringWithFormat:@"%s","00:00"];

Same as writing:

stopwatch.text = @"00:00";
CodeSmile
  • 64,284
  • 20
  • 132
  • 217
  • tried.. same problems.. i don't understand. Before I have tried with custom font my label nodes weren't define which font to use, and it worked well with some default font. Now if I delete line with which font I want label to be written, my app crashes. – MOzeb Nov 04 '14 at 22:10
0

Try this...

-(void)addStopwatch
{
    SKLabelNode *stopwatch = [SKLabelNode labelNodeWithFontNamed:@"Arial"];
    // This positions the label node in the scene
    stopwatch.position = CGPointMake(self.frame.size.width/2,self.frame.size.height/2);
    stopwatch.fontSize = 25;
    stopwatch.fontColor = [UIColor whiteColor];
    stopwatch.text = [NSString stringWithFormat:@"%s","00:00"];
    stopwatch.name = @"stopW";
    [self addChild:stopwatch];
}

EDIT: The issue wasn't that the variable was a global. The label wasn't visible because it was not in the scene (y = self.frame.size.height). You can access the label by name from other parts of your code by

SKLabelNode *label = (SKLabelNode *)[self childNodeWithName:@"stopW"];
label.text = ...
0x141E
  • 12,613
  • 2
  • 41
  • 54
  • it works. But if I want to declare stopwatch node global, so i can change its value and other stuff, it won't work? – MOzeb Nov 05 '14 at 18:49