728x90 AdSpace

Latest Article



Timer Objective-C Issue cocos2d-x ?





I am trying to display some alerts on screen after random amount of time. Following is the obj-C code for this. I am unable to convert it into C++. If anyone has used this technique, please help.

float delay = (float)(rand()%100)/100.0f*7.0f+3.0f; 
CCTimer *timer = [NSTimer scheduledTimerWithTimeInterval:delay target:self selector:@selector(displayRockAlert) userInfo:nil repeats:NO];


SOLUTION:

There is a CCScheduler class that is used in cocos2d to schedule CCTimers. If your class is a subclass of CCNode you can use the CCNode schedule() and unschedule() these timers. Assuming your class's name is GameLayer you'd write
schedule(schedule_selector(GameLayer::displayRockAlert), delay);to have your GameLayer displayRockAlert method called repeatedly after delay seconds.

Note that you'll need to unschedule it too with
unschedule(schedule_selector(GameLayer::displayRockAlert)); once you are in your displayRockAlert method

Also note that your displayRockAlert method should receive a single parameter of type ccTime if you're using this approach

no image
  • Title : Timer Objective-C Issue cocos2d-x ?
  • Posted by :
  • Date : 11:14
  • Labels :






  • Blogger Comments
  • Facebook Comments

0 comments:

Post a Comment