如何更改SKLabelNode的文本? [英] How to change text of a SKLabelNode?
问题描述
如何更新在initWithSize中创建的标签文本? 这是initWithSize中标签的代码:
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.
推荐答案
您将不得不在头文件中声明scoreLabel
,然后在所需的任何位置声明scoreLabel.text = //Your text
.
You will have to declare scoreLabel
in the header file and then just so scoreLabel.text = //Your text
anywhere you want.
在您的.h文件中,声明
@property (nonatomic,strong) SKLabelNode *scoreLabel;
In your .h file, declare
@property (nonatomic,strong) SKLabelNode *scoreLabel;
在您的.m文件中,添加
@synthesize scoreLabel;
In your .m file, add
@synthesize scoreLabel;
初始化标签时
self.scoreLabel = [SKLabelNode labelNodeWithFontNamed:@"TimesNewRoman"];
不是
SKLabelNode *scoreLabel = [SKLabelNode labelNodeWithFontNamed:@"TimesNewRoman"];
这篇关于如何更改SKLabelNode的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!