在c++中增加代码执行延迟
Add delay to code execution in C++
假设我需要调用一个延迟2秒的函数。在cocos2d-x中,你可以使用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);
但是为了运行这个动作,你需要一个Node
,它并不总是可用的。有些课程与Node
无关。所以我想知道在c++ 11中添加延迟代码执行的跨平台方式是什么?
看起来这个问题已经结束了,无论如何你可以使用Scheduler来安排你的方法在Cocos2dx中的任何时候被调用。在返回true之前,在类的init方法中执行如下操作:
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时间。
你可以使用ussleep函数。
必须包含
#include <unistd.h>
在你的类中,在你需要暂停的地方,你可以像这样放置ussleep函数。
usleep(5000000);
,然后调用你需要运行的函数
这将使你的游戏循环进入5秒睡眠模式。
您可以使用c++ 11线程库中添加的std::this_thread::sleep_for
函数:
std::chrono::seconds duration( 2 );
std::this_thread::sleep_for( duration ); // Sleep for 2 seconds.
这将导致当前线程在duration
对象中指定的持续时间内停止执行。
相关文章:
- 在执行其他功能的同时播放动画(LED矩阵和Arduino/ESP8266)
- C++,系统无法执行指定的程序
- 使用C++中的模板和运算符重载执行矩阵运算
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 如何仅为一个函数添加延迟
- 执行函数时导致崩溃的变量
- 无论条件是否为true,if总是在c++中执行
- 将自定义函数传递到基抽象类中以延迟执行
- 异步 60 秒延迟后C++执行函数?
- 如果 QApplication 执行延迟,QWebEngineView 在加载内容时会以静默方式失败
- C++11 延迟执行的"correct"方法
- 在vsync打开的情况下执行glTexImage2D时出现奇怪的延迟
- Lua/C++/luabind /与其他 lua 行为脚本交互中成员函数的执行延迟
- 有没有办法创建一个线程,但在不使用运算符的情况下使用 C++11 延迟它的执行=
- 在不使用睡眠的情况下在延迟之后执行C++中的函数
- 在程序继续执行的同时,如何使代码的两部分之间存在时间间隔或延迟?C++
- 延迟执行1秒
- 在c++中增加代码执行延迟
- 延迟执行代码
- 如何对接口的引用执行延迟计算(尝试使用bind)