Search code examples
iossprite-kitsklabelnode

Changing label text on SKLabelNode crashes app


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?


Solution

  • You are sending the selector "label" to an SKLabelNode, which doesn't recognize it. The touched node is actually an SkLabelNode, and so you are sending "label" to an SkLabelNode when you are trying to set the text at btn.label.text = @"d";