Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cocos2d: How to set a timer

I am developing an iPhone app using cocos2d and box2d.In this app i require to set a timer. The timer will show the remaining time in hand of an player to reach destination...

how can i do that.....i have drawn a scene but no sure as i am beginner how to add timer..

thanks

like image 649
Rony Avatar asked Aug 01 '10 08:08

Rony


2 Answers

I would simply schedule a selector with an interval. This works in all CCNode based classes.

Schedule a selector triggered once per second:

[self schedule:@selector(timerUpdate:) interval:1];

This method gets called once per second:

-(void) timerUpdate:(ccTime)delta
{
  numSeconds++;
  // update timer here, using numSeconds
}

Parceval's method using CCTimer is ok too but you should prefer the static autorelease initializer like this:

CCTimer *myTimer = [CCTimer timerWithTarget:self
                                   selector:@selector(myTimedMethod:)
                                   interval:delay]];
like image 73
LearnCocos2D Avatar answered Sep 20 '22 19:09

LearnCocos2D


You could use CCTimer. Just like this:

float delay = 1.0; // Number of seconds between each call of myTimedMethod:
CCTimer *myTimer = [[CCTimer alloc] initWithTarget:self 
                             selector:@selector(myTimedMethod:) interval:delay]];

The method myTimedMethod: will get called then each second.

like image 31
parceval Avatar answered Sep 22 '22 19:09

parceval