Cocos2d-X v3.x : Calling Functions after Delay

I had been facing some issue with calling functions after some delay. So there have been syntax changes happening in cocos2dx, because of which it was a bit difficult to find out which one was the correct approach.

I finally got the below sequence to work:

  float delay = 2.0f;
  auto delayAction = DelayTime::create(delay);  // For 2 Seconds of Delay
  auto funcCallback = CallFunc::create([](){ 
    // Add Your Code Here!
    // If you want, you can call other functions from here as well
  });
  this->runAction(Sequence::create(delayAction, funcCallback, NULL));

Though initially I didn't like integrating a function within the code (known as lambda function), I realised this made life simpler by removing cumbersome selector calls, where you had to remember separate calls based on what kinds of arguments need to be passed.

Alternatively, you can also use the below option instead:

void ClassName::func1(){
  float delay = 2.0f;
  this->scheduleOnce(schedule_selector(ClassName::func2), delay);
}
void ClassName::func2(float dt){
  // Add Your Code Here!
  // If you want, you can call other functions from here as well
}




Comments

Anonymous said…
Woori Casino Login - Play on Mobile or Desktop
The Woori Casino App will be available 출장안마 at Woori Casino on a mobile or desktop basis. https://octcasino.com/ To play on our mobile or desktop, you can also play with your herzamanindir.com/ desktop browser, หาเงินออนไลน์

Popular posts from this blog

Supporting Unicode and Emoji characters in Unity3D (or other Engine) Game - The Lazy Way

[New] Cocos2DX vs. Unity. What to Choose for 2D Development?