2016-03-10 30 views
0

我想制作像“Ping Pong”这样的小游戏。一切都很好,但现在,当我想添加一个分数时,游戏就会冻结。我用这个代码为我的另一个项目,一切都很好。应用程序在第二点后冻结,目标C

下面是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]; 
} 

在控制台中我得到这个消息:

终止应用程序由于未捕获的异常 'NSInvalidArgumentException',理由是:“Attemped添加SKNode已具有父项:name:'(null)'text:'1'fontName:'ROTORcapExtendedBold'position:{189.33333,106.66666}'

我删除了行scoreLabel == nil后,该应用程序不会冻结,但屏幕上的分数会复制旧分数并使分数无法读取。

我该如何解决?

回答

5

[self addChild:scoreLabel]放入if语句中。

当前,您正试图在每次更新标签时将其添加到场景中,并且一旦它已经在场景中,就不能再次添加它。

+0

啊,是的,愚蠢的错误。感谢您有一个愉快的一天! – artG