应用程序在第二点后冻结,Objective C

App freezes after second point, Objective C

我想制作'Ping Pong'这样的小游戏。一切正常,但现在,当我想添加分数时,游戏冻结了。我将此代码用于我的另一个项目,一切正常。

这是 score 部分的代码:

-(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 语句中。

目前您正在尝试在每次更新标签时将其添加到场景中,并且一旦它已经在场景中就无法再次添加。