I have an in value set to 10. A then run a NSTimer with this code:
[NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(updateTimer) userInfo:nil repeats:YES];
My void look like this
-(void)updateTimer {
timeLeft = timeLeft -0.01;
NSString *string = [NSString stringWithFormat:@"%i",timeLeft];
[timer setString:string];
NSLog(string);
}
I am using Coco2D, this is not the problem
In my log it comes out like this
2012-06-05 17:28:56.030 NumberPop[31291:10a03] 9
2012-06-05 17:28:57.030 NumberPop[31291:10a03] 8
2012-06-05 17:28:58.030 NumberPop[31291:10a03] 7
2012-06-05 17:28:59.030 NumberPop[31291:10a03] 6
2012-06-05 17:29:00.030 NumberPop[31291:10a03] 5
2012-06-05 17:29:01.030 NumberPop[31291:10a03] 4
2012-06-05 17:29:02.030 NumberPop[31291:10a03] 3
2012-06-05 17:29:03.030 NumberPop[31291:10a03] 2
2012-06-05 17:29:04.029 NumberPop[31291:10a03] 1
2012-06-05 17:29:05.029 NumberPop[31291:10a03] 0
2012-06-05 17:29:06.029 NumberPop[31291:10a03] 0
2012-06-05 17:29:07.029 NumberPop[31291:10a03] 0
2012-06-05 17:29:08.030 NumberPop[31291:10a03] 0
2012-06-05 17:29:09.029 NumberPop[31291:10a03] 0
2012-06-05 17:29:10.030 NumberPop[31291:10a03] 0
2012-06-05 17:29:11.029 NumberPop[31291:10a03] 0
2012-06-05 17:29:12.029 NumberPop[31291:10a03] 0
The 0’s keep going on forever.
I am so lost, any help?
So it’s curious as to why you’re trying to subtract .01 from an int. But when you do this, the result gets truncated to an int. So if you subtract .01 from 9 you get 8.99 but int can’t store 8.99 so it gets stored simply as 8. Once you get down to 0 it does the same thing. 0 – .01 = -0.01, which get truncated to 0. So you you’ll always get zero from that assignment.
Instead, try something like this:
timeLeft = timeLeft - 1;or even better
timeLeft --;Either of the above will continue to decrement the integer below zero, ex: -1, -2, -3…. Assuming of course, that it’s an unsigned int (which
intis).Also, if you’re looking to stop the timer at zero, you need to invalidate it when time left reaches zero. Look here for more info: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/nstimer_Class/Reference/NSTimer.html#//apple_ref/occ/instm/NSTimer/invalidate
Basically, do something like this:
There’s something important to understand though. NSTimer doesn’t guarantee that it will be called at exactly the time interval you specify. Basically, it checks on each run loop whether it should fire, and then does so if appropriate. But you can never fire a NSTimer more often than there are run loops. This means if a run loop takes longer than normal or your time interval is too short, it could skip several intervals before it fires. So counting ‘time’ by decrementing a number (like you seem to be doing) won’t work. Instead you’ll need to find another way, like setting a starting NSDate variable when you start the timer and then checking the current time on each timer fire against the start time.
NSDate returns a fairly fine granularity in it’s calculations. So if you take a starting date, you can get the time interval since that date by doing this:
NSTimeInterval interval = [[NSDate date] timeIntervalSinceDate:startDate];NSTimeInterval is a
double. So you can convert it to a string like this:NSString *string = [NSString stringWithFormat:@"%f", interval];or to limit the decimal places:
NSString *string = [NSString stringWithFormat:@"%.2f", interval];This should give you what you’re looking for.