首页 > 代码库 > [cocos2d-x]如何自动发送菜单点击回调事件?
[cocos2d-x]如何自动发送菜单点击回调事件?
我们在工作中经常会碰到一些随机的bug,如果bug发生的概率很低,用手工重现就很麻烦。这时就必须考虑自动化执行有问题的代码段。如果是顺序执行的代码,加上一个循环就可以了。但如果是异步执行的回调事件,就不能简单的加上循环。比如,在httpclient的测试例中,有一个http get的测试菜单,点击一次这个菜单,就会执行下面的回调事件:
void HttpClientTest::onMenuGetTestClicked(cocos2d::CCObject *sender)
{
//test1
......
//test2
{
CCHttpRequest* request = new CCHttpRequest();
// required fields
request->setUrl("http://httpbin.org/ip");
request->setRequestType(CCHttpRequest::kHttpGet);
request->setResponseCallback(this, httpresponse_selector(HttpClientTest::onHttpRequestCompleted));
// optional fields
request->setTag("GET test2");
string empty="empty";
request->setRequestData(empty.c_str(), empty.length());
CCHttpClient::getInstance()->setTimeoutForConnect(1);
CCHttpClient::getInstance()->setTimeoutForRead(1);
CCHttpClient::getInstance()->send(request);
// don‘t forget to release it, pair to new
request->release();
}
//test3
......
// waiting
m_labelStatusCode->setString("waiting...");
}
显然,如果将这个回调函数用while循环包起来,会导致整个测试程序死循环,因为这个回调是在cocos2dx的主线程中执行的。
那么,要怎么做呢?
我的办法是先建立一个工作线程:
// Worker thread
static void* networkThread(void *data)
{
while (true) {
//
CCDirector::sharedDirector()->getScheduler()->scheduleSelector(
schedule_selector(HttpClientTest::dispatchTest),HttpClientTest::getInstance(),0,false);
sleep(1);
break;
}
}
这个线程每隔一秒向cocos2dx的调度器发送一个调度请求,请求的调度事件为dispatchTest,dispatchTest就是原来的onMenuGetTestClicked,然后在onMenuGetTestClicked中启动这个工作线程:void HttpClientTest::onMenuGetTestClicked(cocos2d::CCObject *sender)
{
pthread_create(&s_networkThread,NULL,networkThread,NULL);
pthread_detach(s_networkThread);
}
另外,需要将HttpClientTest改为单例模式,要新增一个getInstance()静态方法:
HttpClientTest* s_pHttpClientTest;
// HttpClient implementation
HttpClientTest* HttpClientTest::getInstance()
{
if (s_pHttpClientTest ==NULL) {
s_pHttpClientTest =newHttpClientTest();
}
returns_pHttpClientTest;
}
这样,点击Http get菜单,就可以实现自动循环发送菜单点击事件了。
[cocos2d-x]如何自动发送菜单点击回调事件?