cocos2dx中有三种定时器:schedule,scheduleUpdate,scheduleOnce。
1、scheduleUpdate 加入当前节点后,程序会每帧都会自动执行一次默认的Update函数。(注:一定是Update函数哦,若想调用其他自己命名的函数则使用schedule) 看例子,走起。 首先在HelloWord类的头文件中声明Update函数: [cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片 void Update(float dt); //注意参数类型 然后在HelloWorld类源文件中实现函数Update: [cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片 void HelloWorld::Update(float dt) { CCLOG("baibai"); } 现在我们可以调用了,在需要他不断执行的地方加入调用的代码就ok: [cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片 this->scheduleUpdate(); //this是当前节点,如layer,所以可以省略啦。 运行之后你将会看到不断有baibai被打印出来 2、scheduleUpdate 可以没隔几秒执行某个自定义的函数,来看代码: 首先还是在HelloWorld中声明所要执行的函数: [cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片 void Move(float dt); 然后在源文件实现: [cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片 void HelloWorld::Move(float dt) { CCLOG("baibai"); } 现在去执行他,注意参数哦 [cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片 scheduleOnce(schedule_selector(HelloWorld::Move),1.0f); //每隔1.0f执行一次,省略参数则表示每帧都要执行 运行之后,baibai每隔1.0f才会被打印一次。 3、scheduleOnce 功能:在几秒之后执行,并且只会执行一次。 我们就执行上面所写的Move函数吧。 [cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片 this->scheduleOnce(schedule_selector(HelloWorld::Move),1.0f); //在1.0f之后执行,并且只执行一次。 运行一下,baibai只是被打印了一次就完了。。。 ok,定时器的调用已经讲完了,大家不妨自己写一些函数体验一下。 但是怎么停止定时器呢? 1、停止执行自己定义的函数定时器: [cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片 this->unschedule(schedule_selector(HelloWorld::Move)); 2、停止默认定时器: [cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片 this->unscheduleUpdate(); 3、停止所有定时器: [cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片 this->unscheduleAllSelectors(); (编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|