cocos2dx 定时器使用 schedule,scheduleUpdate,scheduleOnce

来源:互联网 发布:js正则表达式判断密码 编辑:程序博客网 时间:2024/04/27 16:27

一个游戏中只有一个schedule(是单例),使用的时候要先获取对象,如果继承CCNode 就不用了,因为已经获取了。

CCScheduler *

m_pScheduler = CCDirector::sharedDirector()->getScheduler();

  1. //更新定时器,每帧调用1次。每个节点只能有1个被调度的update函数
  2. void scheduleUpdate(void);
  3. //卸载更新定时器
  4. void unscheduleUpdate(void);
  5. //自定义定时器,如果重复调用,那调用间隔会更新,而不会再次调用
  6. //interval,调用时间间隔,如果为0,建议使用scheduleUpdate
  7. //repeat,回调函数会被执行repeat+1次,kCCRepeatForever是无限次调用
  8. //delay,第一次执行前的延时
  9. void schedule(SEL_SCHEDULE selector, float interval, unsigned int repeat, float delay); 
  10. void schedule(SEL_SCHEDULE selector, float interval); 
  11. void scheduleOnce(SEL_SCHEDULE selector, float delay); 
  12. void schedule(SEL_SCHEDULE selector); 
  13. //卸载自定义定时器
  14. void unschedule(SEL_SCHEDULE selector); 
  15. void unscheduleAllSelectors(void);
  16. //恢复所有定时器和动作,OnEnter调用
  17. void resumeSchedulerAndActions(void);
  18. //暂停所有定时器和动作,OnExit调用
  19. void pauseSchedulerAndActions(void);
  20. //scheduleUpdate每帧调用 注意函数名字
  21. virtual void update(float delta); 





原文地址:http://blog.csdn.net/zhanghefu/article/details/38466801

cocos2dx三种定时器的使用以及停止schedule,scheduleUpdate,scheduleOnce


首先,什么是定时器呢?或许你有时候会想让某个函数不断的去执行,或许只是执行一次,获取你想让他每隔几秒执行一次,ok,这些都可以统统交给定时器来解决。

cocos2dx中有三种定时器:schedule,scheduleUpdate,scheduleOnce。了解其功能便会发现定时器真是太方便了,废话不多说,我们逐一学习一下。

1、scheduleUpdate

加入当前节点后,程序会每帧都会自动执行一次默认的Update函数。(注:一定是Update函数哦,若想调用其他自己命名的函数则使用schedule)

看例子,走起。

首先在HelloWord类的头文件中声明Update函数:

[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. void Update(float dt);   //注意参数类型  
[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. void Update(float dt);   //注意参数类型  
然后在HelloWorld类源文件中实现函数Update:

[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. void HelloWorld::Update(float dt)  
  2. {  
  3.     CCLOG("baibai");  
  4. }  
[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. void HelloWorld::Update(float dt)  
  2. {  
  3.     CCLOG("baibai");  
  4. }  
现在我们可以调用了,在需要他不断执行的地方加入调用的代码就ok:

[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. this->scheduleUpdate();     //this是当前节点,如layer,所以可以省略啦。  
[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. this->scheduleUpdate();     //this是当前节点,如layer,所以可以省略啦。  

运行之后你将会看到不断有baibai被打印出来

2、scheduleUpdate

可以没隔几秒执行某个自定义的函数,来看代码:

首先还是在HelloWorld中声明所要执行的函数:

[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. void Move(float dt);  
[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. void Move(float dt);  
然后在源文件实现:

[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. void HelloWorld::Move(float dt)  
  2. {  
  3.     CCLOG("baibai");  
  4. }  
[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. void HelloWorld::Move(float dt)  
  2. {  
  3.     CCLOG("baibai");  
  4. }  
现在去执行他,注意参数哦

[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. scheduleOnce(schedule_selector(HelloWorld::Move), 1.0f);//每隔1.0f执行一次,省略参数则表示每帧都要执行  
[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. scheduleOnce(schedule_selector(HelloWorld::Move), 1.0f);//每隔1.0f执行一次,省略参数则表示每帧都要执行  

运行之后,baibai每隔1.0f才会被打印一次。

3、scheduleOnce

功能:在几秒之后执行,并且只执行一次。

我们就执行上面所写过的Move函数吧:

[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. scheduleOnce(schedule_selector(HelloWorld::Move), 1.0f); //在1.0f之后执行,并且只执行一次。  
[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. scheduleOnce(schedule_selector(HelloWorld::Move), 1.0f); //在1.0f之后执行,并且只执行一次。  

运行一下,baibai只是被打印了一次就完了。。。

ok,定时器的调用已经讲完,大家不妨自己写一些函数体验一下。

但是怎么让定时器停止呢?

1、停止执行自己定义函数的定时器:

[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. this->unschedule(schedule_selector(HelloWorld::Move));  
[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. this->unschedule(schedule_selector(HelloWorld::Move));  
2、停止默认定时器:

[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. this->unscheduleUpdate();  
[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. this->unscheduleUpdate();  
3、停止所有定时器:

[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. this->unscheduleAllSelectors();  
[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. this->unscheduleAllSelectors();  

1.概况

CCNode内部封装了一个

[cpp] view plaincopy
  1. CCScheduler *m_pScheduler;

正是通过它我们可以很轻松地完成一些定时功能,所以定时器是节点所具备的功能。

定时器分为2种,一种是更新定时器,执行的频率是每帧执行一次,另一种则是自定义回调函数的定时器(最小值是一帧),关于回调函数和函数指针的相关基础可参见http://blog.csdn.net/jackystudio/article/details/11720325。


2.API

[cpp] view plaincopy
  1. //更新定时器,每帧调用1次。每个节点只能有1个被调度的update函数
  2. void scheduleUpdate(void);
  3. //卸载更新定时器
  4. void unscheduleUpdate(void);
  5. //自定义定时器,如果重复调用,那调用间隔会更新,而不会再次调用
  6. //interval,调用时间间隔,如果为0,建议使用scheduleUpdate
  7. //repeat,回调函数会被执行repeat+1次,kCCRepeatForever是无限次调用
  8. //delay,第一次执行前的延时
  9. void schedule(SEL_SCHEDULE selector, float interval, unsigned int repeat, float delay); 
  10. void schedule(SEL_SCHEDULE selector, float interval); 
  11. void scheduleOnce(SEL_SCHEDULE selector, float delay); 
  12. void schedule(SEL_SCHEDULE selector); 
  13. //卸载自定义定时器
  14. void unschedule(SEL_SCHEDULE selector); 
  15. void unscheduleAllSelectors(void);
  16. //恢复所有定时器和动作,OnEnter调用
  17. void resumeSchedulerAndActions(void);
  18. //暂停所有定时器和动作,OnExit调用
  19. void pauseSchedulerAndActions(void);
  20. //scheduleUpdate每帧调用
  21. virtual void update(float delta); 


3.示例

3.1.更新定时器

[cpp] view plaincopy
  1. //开启定时器
  2. this->scheduleUpdate();
  3. //虚函数update
  4. void HelloWorld::update(float delta) 
  5. CCLog("%f",delta);
  6. //输出,这里设置了60fps,调用间隔1/60s
  7. 0.016667 
  8. 0.016676 
  9. 0.016657 
  10. 0.016669 

3.2.自定义定时器

[cpp] view plaincopy
  1. //开启定时器,延时2s执行,执行3+1次,执行间隔1s
  2. this->schedule(schedule_selector(HelloWorld::log),1,3,2);
  3. //回调函数
  4. void HelloWorld::log(float dt) 
  5. CCLog("schedule");
  6. //输出
  7. 2.004532 
  8. 1.005827 
  9. 1.000238 
  10. 1.001019 

4.schedule_selector和SEL_SCHEDULE

看到上面的schedule_selector了吧,这又是个什么玩意?看看它的宏定义。

[cpp] view plaincopy
  1. #define schedule_selector(_SELECTOR) (SEL_SCHEDULE)(&_SELECTOR)
原来是把函数指针转化为SEL_SCHEDULE型指针,那SEL_SCHEDULE又是什么?

[cpp] view plaincopy
  1. typedef void (CCObject::*SEL_SCHEDULE)(float);
也没啥,就是定义了一个带有float参数函数指针。所以我们在使用自定义Schedule的时候,回调函数一定要记得带上一个float参数,它记录了两次执行的间隔。如果忘了,可是会出现类型转换错误的异常。这种方式在callfunc_selector,menu_selector等也以同样的方式出现。


5.谁来调用回调函数

但是有没有发现,如果这个回调函数是个全局函数或者static函数也就算了,偏偏它是个成员函数,成员函数需要实例来调用,可是从调用方法来看,好像没传入调用对象?

[cpp] view plaincopy
  1. this->schedule(schedule_selector(HelloWorld::log),1,3,2);

是的,还记得一开头说的CCNode内部封装的m_pScheduler吗?

[cpp] view plaincopy
  1. CCScheduler *m_pScheduler;
原来CCNode帮我们实现了:

[cpp] view plaincopy
  1. void CCNode::schedule(SEL_SCHEDULE selector, float interval, unsigned int repeat, float delay) 
  2. CCAssert( selector, "Argument must be non-nil"); 
  3. CCAssert( interval >=0, "Argument must be positive"); 
  4. m_pScheduler->scheduleSelector(selector, thisinterval repeat, delay, !m_bRunning); 

原来this这个时候被传入了,同时传入的参数还有m_bRunning,m_bRunning表示节点是否在运行中(是否在舞台上),OnEnter的时候赋值true,OnExit的时候赋值false,所以在执行定时器的时候还必须确保节点有在运行。

这样确实用起来怪怪的,所以在cocos2d-x v3.0版本中,参数和函数指针用一个宏打包起来了~

至于CCSchedule内部是怎么实现的,以及CCTimer的触发回调,有兴趣的就自己看看源码吧
0 0
原创粉丝点击