Say I need to call a function with 2 seconds delay. In cocos2d-x you can use actions:
auto action = Sequence::create(
DelayTime::create(2),
CallFunc::create(
[&]() {
// here is the lambda function that does whatever you want after 2 seconds
}
),
NULL
);
runAction(action);
But in order to run the action you need a Node
, which in not always available. There are classes that have nothing to do with the Node
. So I wonder what is the cross-platform way of adding delay to code execution in C++11?
Looks like this question is closed, anyway you can use Scheduler to schedule your method to get called anytime in Cocos2dx. In your class init method before returning true do something like this.
this->schedule(schedule_selector(HelloWorld::setGamePlaySpeed), .2);
and create a method giving it float dt as argument like this..
void HelloWorld::setGamePlaySpeed(float dt){
// do anything yo want... This method will be Called every .2 seconds
}
Now float dt is the dt time you specified in your scheduler. Hope this Helps anyone looking for same issue in Future...