如何更改SKLabelNode的文本?
如何更新在initWithSize中创建的标签文本? 以下是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];
随着游戏的运行,我更新了变量得分,我只是想知道如何让标签显示新的得分。
你必须在头文件中声明scoreLabel
,然后scoreLabel
在任何你想要的地方使用scoreLabel.text = //Your text
。
编辑:
在.h文件中,声明@property (nonatomic,strong) SKLabelNode *scoreLabel;
在.m文件中,添加@synthesize scoreLabel;
在初始化标签时
self.scoreLabel = [SKLabelNode labelNodeWithFontNamed:@"TimesNewRoman"];
并不是
SKLabelNode *scoreLabel = [SKLabelNode labelNodeWithFontNamed:@"TimesNewRoman"];
为SKLabelNode设置retain / strong属性。 然后从您的应用程序中调用任何位置
@Property(nonatomic,retain)SKLabelNode *scoreLabel;
如果你的分数值有字符串
self.scoreLabel.Text=yourscore
如果是整数
self.scoreLabel.Text=[NSString stringWithFormat:@"%d", [yourscore intValue]]];