【问题标题】:Score label node disappears after first round第一轮后分数标签节点消失
【发布时间】:2016-05-03 10:24:07
【问题描述】:

我不知道为什么,但是第一轮后分数标签节点消失了。我有两个分数,一个是人类的,另一个是计算机的:

-(void)scoreCount{ 
if(scoreLabel == nil){

    NSString* scoretxt =[NSString stringWithFormat:@"0"];
    [scoreLabel setText:scoretxt];

    scoreLabel = [SKLabelNode labelNodeWithFontNamed:@"ROTORcapExtendedBold"];
    scoreLabel.fontSize = 65.f;
    scoreLabel.fontColor = [UIColor grayColor];
    scoreLabel.position =  CGPointMake(CGRectGetMidX(self.frame)/2,CGRectGetMaxY(self.frame)-70);
    scoreLabel.zPosition = -1;
    [self addChild:scoreLabel];
}

scoreLabel.text = [NSString stringWithFormat:@"%ld",(long)score];


if(scoreLabelCom == nil){

    NSString* scoretxtcom =[NSString stringWithFormat:@"0"];
    [scoreLabelCom setText:scoretxtcom];

    scoreLabelCom = [SKLabelNode labelNodeWithFontNamed:@"ROTORcapExtendedBold"];
    scoreLabelCom.fontSize = 65.f;
    scoreLabelCom.fontColor = [UIColor grayColor];
    scoreLabelCom.position =  CGPointMake(CGRectGetMidX(self.frame)+(CGRectGetMidX(self.frame)/2),CGRectGetMaxY(self.frame)-70);
    scoreLabelCom.zPosition = -1;
    [self addChild:scoreLabelCom];
}

scoreLabelCom.text = [NSString stringWithFormat:@"%ld",(long)scoreCom];    
 }

每次有人得分时都会调用这个方法,我把它放进去

-(void)update:(CFTimeInterval)currentTime {
 [self scoreCount];
 }

因为没有它 scoreCount 不会显示 0 分,而只会在第一个点之后显示,但是,当新一轮开始时,ScoreCout 根本不会显示。 我该如何纠正它?为什么会这样?

【问题讨论】:

  • 每次有人得分时都会调用此方法 - 这不是真的。它每秒被调用 60 次,因为您在 update: 方法中运行它。如果您仅在分数增加时才真正更改标签的文本会更好。我的意思是,它可能不会损害您应用的性能,但也没有必要。
  • 当我说它可能不会影响您应用的性能时,这并不意味着您不应该修复。做这样的不良行为最终可能会影响你的应用程序的性能。请参阅此示例,了解如何以高性能方式实现它:stackoverflow.com/a/34841022/3402095
  • 好的,会那样做,但首先,我仍然想弄清楚如何解决我在帖子中难过的问题

标签: ios objective-c count sprite-kit sklabelnode


【解决方案1】:

这个(long)score(long)scoreCom 你现在可以为它增加价值了。

score = score + 1; //Before add to nsstring
scoreLabel.text = [NSString stringWithFormat:@"%ld",(long)score];

scoreCom = scoreCom + 1;//Before add to nsstring
scoreLabelCom.text = [NSString stringWithFormat:@"%ld",(long)scoreCom];

【讨论】:

    【解决方案2】:

    嗯,我不知道它有多好,但我在游戏结束时在didBeginContact添加scoreLabel = nilscoreLabelCom = nil,它现在可以工作了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-24
      • 1970-01-01
      • 2021-11-19
      • 2020-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-18
      相关资源
      最近更新 更多