I am trying to create a simple Countdown Timer so that when a player enters my game the timer begins from 60 down to 0. It seems simple but I am getting confused at how I write this.
So far I have created a method within my GameController.m that looks like this:
-(int)countDownTimer:(NSTimer *)timer {
[NSTimer scheduledTimerWithTimeInterval:-1
invocation:NULL
repeats:YES];
reduceCountdown = -1;
int countdown = [[timer userInfo] reduceCountdown];
if (countdown <= 0) {
[timer invalidate];
}
return time;
}
At the start of the game I initialise the integer Time at 60. The label is then being set within ViewController. But at the moment when I compile the code it just shows the label at 60 and doesn't decrement at all.
Any help would be greatly appreciated - I am new to Objective-C.
EDIT
With some assistance from I have now separated the code into 2 separate methods. The code now looks like this:
-(void)countDown:(NSTimer *)timer {
if (--time == 0) {
[timer invalidate];
NSLog(@"It's working!!!");
}
}
-(void)countDownTimer:(NSTimer *)timer {
NSLog(@"Hello");
[NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:@selector(countDown:)
userInfo:nil
repeats:YES];
}
HOWEVER, the code is still not running properly and when I call the method [game countDownTimer] from my View Controller it breaks saying: "unrecognized selector sent to instance". Can anybody explain what is wrong here?
Several things are wrong with your code:
NULL
You should call the overload that takes a selector, and pass 1
for the interval, rather than -1
.
Declare NSTimer *timer
and int remainingCounts
, then add
timer = [NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:@selector(countDown)
userInfo:nil
repeats:YES];
remainingCounts = 60;
to the place where you want to start the countdown. Then add the countDown method itself:
-(void)countDown {
if (--remainingCounts == 0) {
[timer invalidate];
}
}
Try this
- (void)startCountdown
{
_counter = 60;
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:@selector(countdownTimer:)
userInfo:nil
repeats:YES];
}
- (void)countdownTimer:(NSTimer *)timer
{
_counter--;
if (_counter <= 0) {
[timer invalidate];
// Here the counter is 0 and you can take call another method to take action
[self handleCountdownFinished];
}
}
From the question you posed.you can achieve that by invoking a function for every 1 sec and handle the decrement logic in that.
Snippet:-
NSTimer *t = [NSTimer scheduledTimerWithTimeInterval: 1.0
target: self
selector:@selector(onTick:)
userInfo: nil repeats:YES];
(void)onTick
{
//do what ever you want
NSLog(@"i am called for every 1 sec");
//invalidate after 60 sec [timer invalidate];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With