Search code examples
variablestimernstimer

NSTimer with variable


In my program, I want the length of the timer to become smaller every time it is used. To do this, I put a variable in the NSTimer and that variable is multiplied by 0.9 after the timer is ran. When the "count=count0.9" is commented out, then the program runs fine but the timer does become smaller of course. When it is not commented out, the timer fires (or starts) the GameOver function immediately after the timer starts for the fourth time. It is quite odd.

NSTimer*Timer;
int count=3;

-(void)InGame{
Timer = [NSTimer scheduledTimerWithTimeInterval:count target: self selector:@selector(GameOver)userInfo:(nil) repeats:NO];

count=count*.9; }

It's driving me crazy, I've made a stack overflow account just for this. Thank you for your help! And I wish I knew how to attack my whole code to this for you.


Solution

  • It's probably because you're using an int to store the result so your decimal value is getting truncated.

    Time 1: 3
    Time 2: 3 * .9 = 2.7 => int = 2
    Time 3: 2 * .9 = 1.8 => int = 1
    Time 4: 1 * .9 = 0.9 => int = 0