Objective-C:在SpriteKit中为计时器增加10秒 [英] Objective-C: Adding 10 seconds to timer in SpriteKit

查看:129
本文介绍了Objective-C:在SpriteKit中为计时器增加10秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用别人的代码在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秒.

我已经尝试过通过添加一个称为_countTime的常量实例变量来最初保持60秒来执行此操作.在-init方法中,我调用了[self createTimerWithDuration:_countTime position:_centerOfScreen andSize:24];在此函数中,每当秒"减少时,我将减小_countTime,换句话说,每秒钟,_countTime将减少.当我运行该块时,该块将时间增加10秒,我将删除_countdownClock,将_countTime添加10秒,最后再次调用createTimerWithDuration:position:andSize:,并更新_countTime./p>

但这似乎不适用于我.我认为它会很好地工作. did 将时间增加了10秒,就像我想要的那样,但是计时器将开始减少三分之二.它会等待一秒钟,然后15-14-12 BAM!然后等待一秒钟,然后等待11-10-9 BAM!依此类推.

那么这是怎么回事?这是正确的方法吗?是我有一种更好的方法来增加时间,还是( 更好! )是一种创建具有这种功能的计时器的更好方法?

解决方案

我相信问题是因为您正在自我"上执行操作.您以前的操作并没有被删除,而是仍在每秒浪费时间.试试这个...

[_countdownClock runAction:[SKAction repeatAction:updateLabelAndWait count:seconds] completion:^{
    _countdownClock.text = @"GAME OVER!";
    _gameOver = YES;
    [self runAction:_gameOverSound];
}];

最后调用createTimerWithDuration:position:andSize:

我假设您要在再次调用之前删除旧标签,否则您会得到一些看起来很奇怪的文本.当您从其父级删除_countdownClock时,它也应该删除操作,并且不会持续减少时间,并且应该解决您的问题.

希望这会有所帮助.

I used someone else's code for writing a timer in SpriteKit, and tweaked it a bit. Here's what my code looks like:

- (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];
    }];
}

What I want to happen, is when a certain block of code is run (which I've taken care of by myself), I want to add 10 seconds to the timer.

I tried doing this, already, by adding a constant instance variable called _countTime to hold 60 seconds initially. In the -init method, I called [self createTimerWithDuration:_countTime position:_centerOfScreen andSize:24]; Inside this function, I would decrease the _countTime every time the "seconds" would decrease – in other words, every second, the _countTime would decrease. When I had the block run, the block to add 10 seconds to the time, I would remove the _countdownClock, add 10 seconds to the _countTime, and finally call createTimerWithDuration:position:andSize: again, with the update _countTime.

But this did't seem to work for me. I figured it would work fairly well. It did add 10 seconds to the time, exactly like I wanted it to, but the timer would start going down by threes. It would wait a second, then 15-14-12 BAM! Then wait a second, then 11-10-9 BAM! And so on.

So what's going on here? Was this the right approach? Is there a better way for me to increase the time, or (better yet!) a better way to create a timer, that has a function like this?

解决方案

I believe the issue is because you are running the action on "self". Your old action is not getting removed and it is still removing time every second. Try this...

[_countdownClock runAction:[SKAction repeatAction:updateLabelAndWait count:seconds] completion:^{
    _countdownClock.text = @"GAME OVER!";
    _gameOver = YES;
    [self runAction:_gameOverSound];
}];

and finally call createTimerWithDuration:position:andSize:

I assume you are removing the old label before you called that again otherwise you would get some really odd looking text. When you remove _countdownClock form its parent it should also remove the action and it won't keep decreasing the time and should fix your issue.

Hopefully that helps.

这篇关于Objective-C:在SpriteKit中为计时器增加10秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆