假设我需要延迟 2 秒调用一个函数。在 cocos2d-x 中你可以使用 Action :
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);
但为了运行操作,您需要一个 Node
,它并不总是可用的。有些类与 Node
无关。 所以我想知道在 C++11 中为代码执行添加延迟的跨平台方式是什么?
最佳答案
看起来这个问题已经结束了,无论如何你可以使用 Scheduler 来安排你的方法在 Cocos2dx 中随时被调用。 在您的类 init 方法中,在返回 true 之前执行类似这样的操作。
this->schedule(schedule_selector(HelloWorld::setGamePlaySpeed), .2);
并创建一个方法,将它作为 float dt 作为参数,如下所示。
void HelloWorld::setGamePlaySpeed(float dt){
// do anything yo want... This method will be Called every .2 seconds
}
现在 float dt 是您在调度程序中指定的 dt 时间。 希望这可以帮助任何人在未来寻找同样的问题......
关于c++ - 在 C++ 中为代码执行添加延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24662700/