How to change text of a SKLabelNode?

倖福魔咒の 提交于 2019-12-24 06:06:08

问题


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.


回答1:


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




回答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]]];


来源:https://stackoverflow.com/questions/21621309/how-to-change-text-of-a-sklabelnode

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