我正在使用SKLabelNode
文本并开发一个游戏,如果它是正确的移动,那么标签进入屏幕并显示正确,如果错误那么它应该显示“错误”。我正在使用物理集成并对身体碰撞执行操作。基于我想要更改标签节点文本的操作。
所有操作都在触摸上执行 SpriteKit 中的方法。我创建了这段代码:
SKNode *myLableNode =[self childNodeWithName: labelNodeName];
现在我无法访问Sprite Label的text属性以在运行时更改。就像myLabelNode.text中的touches开始方法一样。
请帮忙。
答案 0 :(得分:4)
这显示了如何动态更改文本
@implementation MyScene
{
SKLabelNode *_words;
}
-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
/* Setup your scene here */
_words = [SKLabelNode labelNodeWithFontNamed:@"Arial"];
_words.fontSize = 30;
_words.fontColor = [SKColor yellowColor];
_words.position = CGPointMake(150,150);
_words.text = @"HELLO";
[self addChild:_words];
self.backgroundColor = [SKColor colorWithRed:0.15 green:0.15 blue:0.3 alpha:1.0];
}
return self;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in touches) {
_words.text = @"WORLD";
}
}
答案 1 :(得分:-2)
它是一个标签属性,你也可以给它一个唯一的标签。
UILabel *label = =[UILabel alloc]init];
label.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGesture =
[[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTap)] autorelease];
[label addGestureRecognizer:tapGesture];
-Below是标签触摸的方法。
//touches begin method
-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event;
//touches end method
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
if (CGRectContainsPoint([self.site frame], [touch locationInView:self.view])){
//do whatever you want
}
}
//touches move method
- (void)touchesMove:(NSSet *)touches withEvent:(UIEvent *)event;