2

How do I update a label's text that was created in initWithSize? Here is the code for the label within the initWithSize:

SKLabelNode *scoreLabel = [SKLabelNode labelNodeWithFontNamed:@"TimesNewRoman"];
    scoreLabel.text = [NSString stringWithFormat:@"%i", score];
    scoreLabel.fontSize = 30;
    scoreLabel.position = CGPointMake(290, CGRectGetMidY(self.frame) - 30);
    scoreLabel.zRotation = -M_PI/2;
    [self addChild:scoreLabel];

As the game is running I update the variable score and I was just wondering how I can get the label to display the new score.

Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247
user2975241
  • 73
  • 2
  • 7
  • post the code how you are actually trying to change the label text, the errors you mention in comments indicate that the problem is in properly referencing the label object – CodeSmile Feb 07 '14 at 07:41

2 Answers2

2

You will have to declare scoreLabel in the header file and then just so scoreLabel.text = //Your text anywhere you want.

EDIT:

In your .h file, declare @property (nonatomic,strong) SKLabelNode *scoreLabel;

In your .m file, add @synthesize scoreLabel;

When you are initializing the label do

self.scoreLabel = [SKLabelNode labelNodeWithFontNamed:@"TimesNewRoman"];

and not

SKLabelNode *scoreLabel = [SKLabelNode labelNodeWithFontNamed:@"TimesNewRoman"];

Divya
  • 818
  • 8
  • 10
  • Thanks, but where would I add attributes like fontsize, position, etc? Because now I get a warning saying "local declaration of scoreLabel hides instance variable" – user2975241 Feb 07 '14 at 07:19
  • Whenever the score is update just call a method say :`adjustScoreLabel`. In that ` - (void)adjustScoreLabel{ scoreLabel.text = //Your Text; scoreLabel.fontSize = //Your fontSize; scoreLabel.position = // Your position; // whatever attributes of the scoreLabel you want to change, add them here. }` – Divya Feb 07 '14 at 07:22
  • I can edit scoreLabel.text but I get an error if I try to put scoreLabel.position or scoreLabel.fontsize. The error is "Property 'fontsize' not found on object of type UILabel." When I declare scoreLabel in the header file, I'm not actually creating the label on the screen am I? – user2975241 Feb 07 '14 at 07:36
  • No you are just declaring it in the header file. It is actually added to the screen when you do `[self.view addSubview:self.scoreLabel];`.Also, you are getting the error because there is no property called fontSize in UILabel.If you go to the SKLabelNode .h file, is a property called fontSize declared? If no, then to change the fontSize just do `self.scoreLabel.font = [UIFont fontWithName:/*fontName*/ size:/*fontSize*/];` – Divya Feb 07 '14 at 07:53
2

Set retain/strong property for your SKLabelNode. then call any where from your app just

@Property(nonatomic,retain)SKLabelNode *scoreLabel;

if your score value has string

self.scoreLabel.Text=yourscore

if it is integer

self.scoreLabel.Text=[NSString stringWithFormat:@"%d", [yourscore intValue]]];
Pugin
  • 346
  • 1
  • 6
  • 19
codercat
  • 22,873
  • 9
  • 61
  • 85
  • If your adding the SKLabelNode to your SKScene then you can set the @property to weak as it will be retained by the SKScene when you add the label as a child. – fuzzygoat Feb 07 '14 at 08:46