Iphone – cocos2d: How to set a timer

cocos2d-iphoneiphone

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

Best Answer

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