scheduleOnce problem

I am scheduling a event like this (in cocos2d-2.0-x-2.0.4):

this->scheduleOnce(schedule_selector(HelloWorld::callBack), 1.0);

Then, in the callback:

`void HelloWorld::callBack(float dt)
{

// some stuff

float randomInterval = getRandomInterval();
this->scheduleOnce(schedule_selector(HelloWorld::callBack), randomInterval());

}`

As a result, the callBack() is called only once and then it prints:

CCScheduler#scheduleSelector. Selector already scheduled. Updating interval from: 0.0000 to 0.0000

Adding unschedule:

@ this->unschedule(schedule_selector(HelloWorld::callBack));@

does not change the behavior, except that no log is printed out.

Am I doing something wrong, or is this a known issue?

Thank you!

I think it isn’t an issue. In HelloWorld::callBack, the scheduleOnce didn’t finish, and you started it again. So the Warning would appear.
The approach that to unschedule it is right. But even if you don’t invoke unschedule, it’s ok too.

No reason but I prefer to use CCDelay function rather than using scheduleOnce.

For example:-

CCDelayTime starDelay = CCDelayTime::create;
CCCallFunc
showHearts = CCCallFunc::create(this, callfunc_selector(GameBoardScene::placeStarAction));
CCSequence* newSeq = (CCSequence*)CCSequence::create(starDelay, showHearts, NULL);
starSprite->runAction(newSeq);

And if you are calling the function in loop but like to run it once then I would add a parameter to check whether action is already initiated or not.

I would do it like this:

void HelloWorld::recursiveFunc( cocos2d::CCObject * p_Sender  ) {
   // Check if should run function
   if ( ! this -> doSpawnMonsters() ) return;

   // Do something
   this -> createMonsterAt( CCPointMake(100, 100 ) );

   // Get random time between 1 seconds and 3 seconds
   float randTime = MathHelper::getRandomNumber( 1, 3 );

   // Have the layer run the action and call this same method again
   CCDelayTime * delayAction = CCDelayTime::create( randTime );
   CCCallFunc * callFunc = CCCallFunc::create( this, callfunc_selector( HelloWorld::recursiveFunc );
   this -> runAction( CCSequence::createWithTwoActions( delayAction, callFunc ) );
}
1 Like