如何在cocos2d-x场景中的另一个线程中运行自定义网络代码?我在cocos2d-x和SDL_Net上进行回合制多人游戏。
答案 0 :(得分:1)
使用std::thread
创建和分离第二个帖子:
#include <chrono>
#include <thread>
#include <iostream>
int main(int argc, char *argv[])
{
std::thread thread_name([]()
{
while (true)
{
std::this_thread::sleep_for(std::chrono::seconds(2));
std::cout << "Hello from other thread" << std::endl;
}
});
thread_name.detach();
std::this_thread::sleep_for(std::chrono::seconds(11));
return 0;
}
使用cocos2d::Scheduler
来安排该线程的操作:
cocos2d::Director::getInstance()->getScheduler()->schedule([](float)
{
std::cout << "We can operate cocos-related things here" << std::endl;
}, this, 0.0f, 1, 0, false, "name");