标签不改变倒计时器SpriteKit Objective C?

时间:2014-11-29 13:52:04

标签: objective-c sprite-kit sklabelnode

编辑2号,好的,我想我现在已经把它煮到了这一点。我已经使用了你的所有建议,并使用断点进行测试,谢谢。

我需要做的最后一点是运行此等待操作。

if (timerStarted == YES) {

    [countDown runAction:[SKAction waitForDuration:1]];
    if (countDownInt > 0) {
    countDown.text = [NSString stringWithFormat:@"%i", countDownInt];
    countDownInt = countDownInt - 1.0;
    [self Timer];

    }else{
        countDown.text = [NSString stringWithFormat:@"Time Up!"];
    }

runAction:部分似乎不起作用。我猜这是因为我选择了错误的节点来代替(SKLabelNode" countDown")。我可以使用哪个节点来运行此代码?

感谢所有迄今为止帮助过的人

2 个答案:

答案 0 :(得分:4)

以下是如何在SpriteKit中实现倒数计时器的示例。

首先,声明一个创建1)标签节点以显示剩余时间的方法,以及2)更新标签的适当操作,等待一秒钟,并冲洗/泡沫/重复

- (void) createTimerWithDuration:(NSInteger)seconds position:(CGPoint)position andSize:(CGFloat)size {
    // Allocate/initialize the label node
    countDown = [SKLabelNode labelNodeWithFontNamed:@"Chalkduster"];
    countDown.position = position;
    countDown.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeLeft;
    countDown.fontSize = size;
    [self addChild: countDown];
    // Initialize the countdown variable
    countDownInt = seconds;
    // Define the actions
    SKAction *updateLabel = [SKAction runBlock:^{
        countDown.text = [NSString stringWithFormat:@"Time Left: %ld", countDownInt];
        --countDownInt;
    }];
    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:^{
        countDown.text = @"Time's Up";
    }];
}

然后使用持续时间(以秒为单位)和标签的位置/大小调用方法。

CGPoint location = CGPointMake (CGRectGetMidX(self.view.frame),CGRectGetMidY(self.view.frame));
[self createTimerWithDuration:20 position:location andSize:24.0];

答案 1 :(得分:1)

我不会使用更新方法。使用SKActions制作计时器。例如

id wait = [SKAction waitForDuration:1];
id run = [SKAction runBlock:^{
    // After a second this is called
}];
[node runAction:[SKAction sequence:@[wait, run]]];

即使只运行一次,如果你想每秒或任何时间间隔调用,你总是可以将它嵌入到SKActionRepeatForever中。 资源: SpriteKit - Creating a timer