【发布时间】:2016-02-23 07:54:31
【问题描述】:
在我的代码中,我正在尝试更新标签 (SKSpriteNode),同时使用 sprite 工具包倒计时,但我不想使用 NSTimer,所以我找到了这个解决方案,但问题是 waitForDuration 方法仅适用第一次运行更新方法。我想使用 sprite kit 每秒运行一段代码(更新标签和变量)。任何帮助表示赞赏。
-(void)update:(CFTimeInterval)currentTime {
id wait = [SKAction waitForDuration:1.0];
id run = [SKAction runBlock:^{
remainingCounts = remainingCounts - 1;
[progressBar setProgress:remainingCounts/10];
timerLabelNode.text =[NSString stringWithFormat:@"Time: %d", remainingCounts];
if (--remainingCounts == 0) {
_AreYouLost=YES;
if (_score>[savedBestScore intValue]) {
[self updateBestScore:[NSMutableString stringWithFormat:@"%d",_score]];
}
[self updateBestScore:[NSMutableString stringWithFormat:@"%d",_score]];
SKScene *GameovrScn = [GameOverScene sceneWithSize:self.view.bounds.size];
GameovrScn.scaleMode = SKSceneScaleModeAspectFill;
[self.view presentScene:GameovrScn transition:[SKTransition doorwayWithDuration:0.4]];
NSLog(@"Time is up!!!");
}
}];
}
【问题讨论】:
-
您使用操作使这变得非常复杂。 update 方法是直接跟踪时间。您是否考虑过只使用更新方法中的时间进行倒计时?
-
我想说只是将你的倒计时代码移动到一个单独的函数中。您不需要在
update中运行它。 -
好的,但是我应该在哪里调用它以便每秒调用一次? @ChrisSlowik 我应该在更新方法中调用它吗?
-
看看下面的答案
-
@Reza.Ab 我现在看到的代码实际上什么也没做——你在每个更新调用中创建了两个动作,但你从不运行它们。 SKAction runBlock:方法,实际上创建一个执行块的动作,而不是在调用时直接执行该块。您的代码的另一个问题是您试图每秒运行代码块,但您是在每秒运行 60 次的方法内部执行此操作的......这真的没有意义。您应该使用 trojanfoe 指向的内容,或者您可以使用 SKActions...
标签: ios objective-c sprite-kit