user4103894
user4103894

Reputation:

Label not changing countdown timer SpriteKit Objective C?

Edit no.2 , Ok, I think I have boiled this right down to the point now. I have used all your advice, and tested with breakpoints, so thank you.

The last bit I need to do, is run this wait action.

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!"];
    }

The runAction: section doesn't seem to work. I am guessing this is because I have selected the wrong node to put in the place of the (SKLabelNode "countDown"). Which node could I use to run this code?

Thank you everyone who has helped so far

Upvotes: 2

Views: 956

Answers (2)

0x141E
0x141E

Reputation: 12753

Here's an example of how to implement a countdown timer in SpriteKit.

First, declare a method that creates 1) a label node to display the time left and 2) the appropriate actions to update the label, wait for one second, and rinse/lather/repeat

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

and then call the method with the duration (in seconds) and the label's position/size.

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

Upvotes: 4

SierraMike
SierraMike

Reputation: 1569

I would not use the update method. Use SKActions to make a timer. For an example

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

Even though this will only run once, you can always embed this in an SKActionRepeatForever if you want to be called every second or whatever time interval. Source: SpriteKit - Creating a timer

Upvotes: 1

Related Questions