Objective c 目标C:在SpriteKit中为计时器添加10秒

Objective c 目标C:在SpriteKit中为计时器添加10秒,objective-c,time,timer,sprite-kit,seconds,Objective C,Time,Timer,Sprite Kit,Seconds,我使用其他人的代码在SpriteKit中编写计时器,并对其进行了一些调整。我的代码是这样的: - (void)createTimerWithDuration:(NSInteger)seconds position:(CGPoint)position andSize:(CGFloat)size { // Allocate/initialize the label node. _countdownClock = [SKLabelNode labelNodeWithFontNamed:

我使用其他人的代码在SpriteKit中编写计时器,并对其进行了一些调整。我的代码是这样的:

- (void)createTimerWithDuration:(NSInteger)seconds position:(CGPoint)position andSize:(CGFloat)size
{
    // Allocate/initialize the label node.
    _countdownClock = [SKLabelNode labelNodeWithFontNamed:@"Avenir-Black"];
    _countdownClock.fontColor = [SKColor blackColor];
    _countdownClock.position = position;
    _countdownClock.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeLeft;
    _countdownClock.fontSize = size;
    [self addChild:_countdownClock];

    // Initialize the countdown variable.
    _countdown = seconds;

    // Define the actions.
    SKAction *updateLabel = [SKAction runBlock:^{
        _countdownClock.text = [NSString stringWithFormat:@"Time Left: 0:%lu", (unsigned long)_countdown];
        _countdown--;
    }];

    SKAction *wait = [SKAction waitForDuration:1.0];

    // Create a combined action.
    SKAction *updateLabelAndWait = [SKAction sequence:@[updateLabel, wait]];

    // Run action "seconds" number of times and then set the label to indicate the countdown has ended.
    [self runAction:[SKAction repeatAction:updateLabelAndWait count:seconds] completion:^{
        _countdownClock.text = @"GAME OVER!";
        _gameOver = YES;
        [self runAction:_gameOverSound];
    }];
}
我想做的是,当某个代码块运行时(我自己处理),我想在计时器上加10秒

我已经尝试过这样做了,添加了一个名为
\u countTime
的常量实例变量,最初保持60秒。在-init方法中,我调用了
[self-createTimerWithDuration:_CountTimePosition:_CenterofScreenand Size:24]
在这个函数中,每当“秒”减少时,我都会减少
\u countTime
,换句话说,每秒
\u countTime
都会减少。当我运行块时,我会删除
\u countdownClock
,将10秒添加到
\u countTime
,最后再次调用
createTimerWithDuration:position:and Size:
,并更新
\u countTime

但这似乎对我不起作用。我想它会工作得相当好。它确实给时间增加了10秒,就像我希望的那样,但是计时器开始下降3秒。它会等一秒钟,然后15-14-12砰!等一下,然后11-10-9砰!等等


这是怎么回事?这是正确的方法吗?有没有更好的方法来增加时间,或者(更好!)有没有更好的方法来创建一个计时器,它有这样的功能?

我相信问题是因为你在“自我”上运行操作。您的旧操作不会被删除,它仍然每秒删除一次。试试这个

[_countdownClock runAction:[SKAction repeatAction:updateLabelAndWait count:seconds] completion:^{
    _countdownClock.text = @"GAME OVER!";
    _gameOver = YES;
    [self runAction:_gameOverSound];
}];
最后调用createTimerWithDuration:position:andSize:

我假设您在再次调用之前正在删除旧标签,否则您将得到一些看起来非常奇怪的文本。当您从其父项中删除
\u countdownClock
时,它也应该删除该操作,并且它不会持续减少时间,并且应该解决您的问题


希望这能有所帮助。

哇。。。真不敢相信我居然没想到!当然,在现场运行它会导致问题。很抱歉反应太晚;我有一段时间没有机会做这件事了。但它现在可以正常工作了。我接受了这个答案@克里斯蒂安·莱德很高兴它奏效了。那只虫子会把我逼疯的:)