sahil
sahil

Reputation: 141

Cocos2d schedule interval decreaser

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.

Upvotes: 3

Views: 2147

Answers (1)

Saurabh Passolia
Saurabh Passolia

Reputation: 8109

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]]; 

}

Upvotes: 2

Related Questions