1

我正在SKLabelNode使用文本并开发一个游戏,如果它是正确的移动,那么标签会出现在屏幕内并显示正确,如果错误那么它应该显示“错误”。我正在使用物理集成并对身体碰撞执行操作。根据我想更改标签节点文本的操作。所有动作都在SpriteKit中的 touches started 方法上执行。我创建了这段代码:

SKNode *myLableNode =[self childNodeWithName: labelNodeName]; 

现在我无法访问 Sprite Label 的文本属性以在运行时更改。就像触摸开始方法中的 myLabelNode.text 一样。

请帮忙。

4

2 回答 2

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

    }
}
于 2013-10-04T22:07:32.090 回答
-2

这是一个标签属性,你也可以给一个独特的标签也标签。

UILabel *label = =[UILabel alloc]init];
label.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGesture =
[[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTap)]     autorelease];
[label addGestureRecognizer:tapGesture];

- 以下是标签触摸的方法。

//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;
于 2013-10-04T19:54:45.737 回答