我正在尝试在运行时更改SKLabelNode中的文本,但它崩溃了应用程序并出现错误:CRASH: - [SKLabelNode label]:无法识别的选择器发送到实例
我是用selectNodeForTouch:(CGPoint)touchlocation
方法调用它,如下所示:
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");
}
按钮对象具有以下界面:
@interface button : SKSpriteNode
@property (nonatomic, retain) SKLabelNode *label;
并在button.m文件
中初始化 @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;
}
知道如何在运行时更改此项目上的文本而不会导致应用程序崩溃吗?
答案 0 :(得分:2)
您正在将选择器“label”发送到SKLabelNode,而SKLabelNode无法识别它。触摸的节点实际上是一个SkLabelNode,因此当您尝试在btn.label.text = @“d”处设置文本时,您将向SkLabelNode发送“label”;