cocos2d-x schedule

原创文章,转载请注明出处:http://blog.csdn.net/zhy_cheng/article/details/8274042

使用CCNode的schedule函数可以实现一个定时器,该函数一共有三个重载的函数

[cpp]  view plain copy
  1. void CCNode::unscheduleUpdate()  
  2.  
  3.     m_pScheduler->unscheduleUpdateForTarget(this);  
  4.  
  5.   
  6. void CCNode::schedule(SEL_SCHEDULE selector)  
  7.  
  8.     this->schedule(selector, 0.0f, kCCRepeatForever, 0.0f);  
  9.  
  10.   
  11. void CCNode::schedule(SEL_SCHEDULE selector, float interval)  
  12.  
  13.     this->schedule(selector, interval, kCCRepeatForever, 0.0f);  
  14.  
  15.   
  16. void CCNode::schedule(SEL_SCHEDULE selector, float interval, unsigned int repeat, float delay)  
  17.  
  18.     CCAssert( selector, "Argument must be non-nil");  
  19.     CCAssert( interval >=0, "Argument must be positive");  
  20.   
  21.     m_pScheduler->scheduleSelector(selector, thisinterval, !m_bIsRunning, repeat, delay);  
  22.  
  23.   
  24. void CCNode::scheduleOnce(SEL_SCHEDULE selector, float delay)  
  25.  
  26.     this->schedule(selector, 0.0f, 0, delay);  
  27.  
  28.   
  29. void CCNode::unschedule(SEL_SCHEDULE selector)  
  30.  
  31.     // explicit nil handling  
  32.     if (selector == 0)  
  33.         return 
  34.   
  35.     m_pScheduler->unscheduleSelector(selector, this);  
  36.  
  37.   
  38. void CCNode::unscheduleAllSelectors()  
  39.  
  40.     m_pScheduler->unscheduleAllSelectorsForTarget(this);  
  41.  
  42.   
  43. void CCNode::resumeSchedulerAndActions()  
  44.  
  45.     m_pScheduler->resumeTarget(this);  
  46.     m_pActionManager->resumeTarget(this);  
  47.  
  48.   
  49. void CCNode::pauseSchedulerAndActions()  
  50.  
  51.     m_pScheduler->pauseTarget(this);  
  52.     m_pActionManager->pauseTarget(this);  
  53.  


 


上面的代码是CCNode类中与schedule的代码。

void CCNode::schedule(SEL_SCHEDULE selector, float interval, unsigned int repeat, float delay)

第一个参数是回调函数的指针

第二个参数是间隔多少时间调用一次

第三个参数是调用这个函数多少次

第四个参数是多少时间后调用这个函数

 

void CCNode::schedule(SEL_SCHEDULE selector)

默认每帧调用一次,调用无数次,马上调用

void CCNode::schedule(SEL_SCHEDULE selector, float interval)

调用无数次,马上调用。

 

 

有了定时器,我们就可以让某个函数一直执行下去,也可以然函数延时执行,也可以让函数执行若干次。

可以在init函数中调用这个方法,也可以在onEnter函数中调用这个方法,注意了,在onEnter函数中个调用这个方法的话,要保证调用父类的onEnter方法。

onEnter函数可能被多次调用,但是init是初始化的时候调用。

回调函数没有返回值,有一个float参数,表示距离上次调用该方法的时间长度。

 

[cpp]  view plain copy
  1. void HelloWorld::update(float dt)  
  2.  
  3.       
  4.     char *buf=new char[50];  
  5.       
  6.     memset(buf,0,10);  
  7.     sprintf(buf,"dt=%f",dt);  
  8.     CCLog(buf);  
  9.       
  10.     delete []buf;  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值