ios - Int failing to do math? -
i have in value set 10. run nstimer code: [nstimer scheduledtimerwithtimeinterval:0.01 target:self selector:@selector(updatetimer) userinfo:nil repeats:yes];
void this
-(void)updatetimer { timeleft = timeleft -0.01; nsstring *string = [nsstring stringwithformat:@"%i",timeleft]; [timer setstring:string]; nslog(string); }
i using coco2d, not problem in log comes out 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. lost, help?
so it's curious why you're trying subtract .01 int. when this, result gets truncated int. if subtract .01 9 8.99 int can't store 8.99 gets stored 8. once down 0 same thing. 0 - .01 = -0.01, truncated 0. you'll 0 assignment.
instead, try this:
timeleft = timeleft - 1;
or better
timeleft --;
either of above continue decrement integer below zero, ex: -1, -2, -3.... assuming of course, it's unsigned int (which int
is).
also, if you're looking stop timer @ zero, need invalidate when time left reaches zero. here 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, this:
timeleft --; if (timeleft <= 0) [timer invalidate];
there's important understand though. nstimer doesn't guarantee called @ time interval specify. basically, checks on each run loop whether should fire, , if appropriate. can never fire nstimer more there run loops. means if run loop takes longer normal or time interval short, skip several intervals before fires. counting 'time' decrementing number (like seem doing) won't work. instead you'll need find way, setting starting nsdate variable when start timer , checking current time on each timer fire against start time.
nsdate returns fine granularity in it's calculations. if take starting date, can time interval since date doing this:
nstimeinterval interval = [[nsdate date] timeintervalsincedate:startdate];
nstimeinterval double
. can convert string this:
nsstring *string = [nsstring stringwithformat:@"%f", interval];
or limit decimal places:
nsstring *string = [nsstring stringwithformat:@"%.2f", interval];
this should give you're looking for.
Comments
Post a Comment