我想制作像'Ping Pong'这样的小游戏。一切正常,但现在,当我想添加分数时,游戏卡住了。我将此代码用于我的另一个项目,一切正常。
这是分数
部分的代码:
-(void)scoreCount{
score ++;
if(scoreLabel == nil){
scoreLabel = [SKLabelNode labelNodeWithFontNamed:@"ROTORcapExtendedBold"];
scoreLabel.fontSize = 40;
scoreLabel.position = CGPointMake(self.frame.size.width/2,self.frame.size.height/3);
scoreLabel.zPosition = 0;
}
[self addChild:scoreLabel];
scoreLabel.text = [NSString stringWithFormat:@"%ld",(long)score];
}
在控制台中我收到这条消息:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attemped to add a SKNode which already has a parent: name:'(null)' text:'1' fontName:'ROTORcapExtendedBold' position:{189.33333, 106.66666}'
在我删除行 scoreLabel == nil
后,应用程序不会卡住,但屏幕上的乐谱会复制旧乐谱并使乐谱不可读。
我该如何解决?
最佳答案
将 [self addChild:scoreLabel]
放在 if 语句中。
目前,您每次更新标签时都试图将其添加到场景中,并且一旦它已经在场景中就无法再次添加。
关于ios - 应用程序在第二点后卡住,Objective C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35926369/