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

标签 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”)。我可以使用哪个节点来运行此代码?

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

最佳答案

以下是如何在 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];

关于objective-c - 标签不改变倒计时器 SpriteKit Objective C?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27203022/

相关文章:

ios - 导航时保持计时器在另一个页面上运行

swift - 平面动画开始时几乎没有卡住

ios - 如何在 SpriteKit 的所有设备上使文本大小相同?

ios - SKLabelNode 删除前导和尾随空格 - 我怎样才能阻止它?

ios - 使用 Swift 在 SpriteKit 中将自定义 "number"图像添加到 SKLabelNode 作为分数

ios - 导航栏不占满整个屏幕

ios - 为什么标签/按钮显示在 viewController 转换动画之前?

objective-c - 当应用程序为 "Installing"并且可以以编程方式控制它时,iOS 会做什么?

ios - SpriteKit swift 3 物理体碰撞并仅移除一个节点

ios - 在 SKScene 中更改 SKSpriteNode 的纹理