当我需要更新SKLabelNode时,SKScene无法渲染

时间:2014-02-20 08:21:22

标签: rendering sprite-kit skscene sklabelnode

我正在编写游戏,并使用SKLabel查看当前分数。问题是,当分数改变时,它不会在屏幕上显示当前的变化,它会在一秒钟后或多或少地显示。在使用[sklabelscore setTextScore:++self.score]时,我该怎么做才能看到变化。我可以强制渲染或类似的东西吗?

当用户使用touchesBegan:withEvent:

触及敌人时,我会调用setTextScore

setTextScore:实施

SKLabelNode* scoreLabel=(SKLabelNode*)[self childNodeWithName:@"scoreLabel"];
scoreLabel.text=[NSString stringWithFormat:@"Score: %d",self.score];

2 个答案:

答案 0 :(得分:2)

您增加的++score似乎很可能是一个局部变量,与self.score不一样。

您可以按如下方式调用该方法:

[sklabelscore setTextScore:++score]

这意味着它的代码签名必须是:

-(void) setTextScore:(int)theNewScore
{
    SKLabelNode* scoreLabel=(SKLabelNode*)[self childNodeWithName:@"scoreLabel"];
    scoreLabel.text=[NSString stringWithFormat:@"Score: %d",self.score];
}

所以你传入theNewScore但不是在格式字符串中使用它,而是使用self.score,如果递增的++score变量是局部变量,则可能永远不会更新(即永远不会将其新值分配给self.score)。

答案 1 :(得分:1)

解决了...我觉得自己像个白痴:S

问题是当我触摸它时,我淡出敌人,然后,0.5秒后,更改标签。我把它放在了街区之外,一切正常。

更改了setTextScore:方法因为多余(感谢@ LearnCocos2D)

...
SKAction* fade=[SKAction fadeOutWithDuration:0.5];
[node runAction:fade completion:^{
    [node removeFromParent];
    self.enemyNumber--;
    self.score++;
    SKLabelNode* scoreLabel=(SKLabelNode*)[self childNodeWithName:@"scoreLabel"];
    scoreLabel.text=[NSString stringWithFormat:@"Score: %d",self.score];
}];

新表格(在街区外):

...
self.score++;
SKLabelNode* scoreLabel=(SKLabelNode*)[self childNodeWithName:@"scoreLabel"];
scoreLabel.text=[NSString stringWithFormat:@"Score: %d",self.score];
SKAction* fade=[SKAction fadeOutWithDuration:0.5];
[node runAction:fade completion:^{
    [node removeFromParent];
}];

感谢您的帮助,并抱歉提出这个愚蠢的问题......