0

I'm trying to change the text in a SKLabelNode at runtime but it crashes the app with an error: CRASH: -[SKLabelNode label]: unrecognized selector sent to instance

I'm calling it from a selectNodeForTouch:(CGPoint)touchlocation method as follows:

        if ([[node name]isEqualToString:@"e"]) {
                        // add current riddle to favourites and change the icon of the star
                        button *btn = (button *)[self nodeAtPoint:touchLocation];
                        btn.label.text = @"d";
                        [(GameViewController *)self.view.window.rootViewController addToFavourites:_currentTomhais];
                        NSLog(@"Favourited");
        }

The button object has the following interface:

@interface button : SKSpriteNode
@property (nonatomic, retain) SKLabelNode *label;

And is initialised as followed in the button.m file

    @implementation button
-(instancetype) initWithString:(NSString *)character fontNamed:(NSString *)font size:(float)size{
    self = [super init];
    if (self) {
        [self setSize:CGSizeMake(size, size)];
        //icon Text
        _label = [[SKLabelNode alloc] initWithFontNamed:font];
        _label.name = character;
        _label.fontSize = size;
        _label.fontColor = [UIColor colorWithRed:150.0f/255.0f
                                          green:166.0f/255.0f
                                           blue:173.0f/255.0f
                                          alpha:1];

        [_label setText:character];
        _label.position = CGPointMake(0, 0);
        [self addChild:_label];

    }
    return self;
}

Any idea how to change the text on this item at runtime without crashing the app?

4

1 回答 1

2

您将选择器“标签”发送到无法识别的 SKLabelNode。触摸的节点实际上是一个 SkLabelNode,因此当您尝试在 btn.label.text = @"d"; 处设置文本时,您正在向 SkLabelNode 发送“标签”;

于 2015-03-25T22:50:36.527 回答