3

I have made a simple timer but in trying to increase the timers speed over time, so I basically want the timer to have an interval of 1.0 and every 10 seconds I want that timer's interval to decrease by 0.1

How can I go about doing that?

[self schedule:@selector(tick:)interval:1.0];

That's in the init section

-(void)tick:(ccTime)dt{
myTime = myTime+1;
totalTime = myTime; 

[timeLabel setString:[NSString stringWithFormat:@"%i", totalTime]]; 
}

That's the timer.

It's a basic timer which has an interval of 1.0 second, however I would like the interval to decrease by 10% every 10 seconds.

Bart
  • 19,692
  • 7
  • 68
  • 77
sahil
  • 141
  • 1
  • 9

1 Answers1

2

in .h file

@property(nonatomic,readWrite)CGFloat lastUpdatedInterval;

in.m file, initialize self.lastUpdatedInterval = 1.0;

then call

[self schedule:@selector(tick:)interval:self.lastUpdatedInterval];

in update loop,

-(void)tick:(ccTime)dt
{
    if(myTime>9 && (myTime%10 == 0))
    {
      [self unschedule:@selector(tick:)];
      self.lastUpdatedInterval = self.lastUpdatedInterval - (self.lastUpdatedInterval*0.1);
      [self schedule:@selector(tick:)interval:self.lastUpdatedInterval];
    }

myTime = myTime+1;
totalTime = myTime; 

[timeLabel setString:[NSString stringWithFormat:@"%i", totalTime]]; 

}
Saurabh Passolia
  • 8,099
  • 1
  • 26
  • 41
  • just add further condition to prevent it going negative. – Saurabh Passolia Feb 07 '12 at 19:04
  • 'code' @property(nonatomic,readWrite)CGFloat lastUpdatedInterval; 'code' can you tell me what that does? thanks – sahil Feb 08 '12 at 15:01
  • i tried that but it says: property 'lastUpdatedInterval' requires the method 'setLastUpdatedInterval:' to be defined - use (at)synthesize, (at)dynamic or provide a method implementation. sorry im new to this. isnt there an easier method? – sahil Feb 08 '12 at 16:04
  • in .m file, write @synthesize lastUpdatedInterval; after @ implementation – Saurabh Passolia Feb 08 '12 at 17:48
  • thanks mate, i have got it to work, but my timer seems to skip every number 1! lol! so it would go 1..2..3..4..5..6..7..8..9..10..blink..12..etc – sahil Feb 09 '12 at 16:54
  • any ideas why the timer skips number all the 1's? – sahil Feb 09 '12 at 18:09
  • move this line at the top of the function as first line and try out : myTime = myTime+1; – Saurabh Passolia Feb 09 '12 at 18:18