1.定义

定时器在大部分游戏中是不可或缺的,即每隔一段时间,就要执行相应的刷新体函数,以更新游戏的画面、时间、进度、敌人的指令等等。

cocos2dx为我们提供了定时器schedule相关的操作。其操作函数的定义在CCNode中,所以基本上大多数的引擎类都可以设置定时器,如CCLayer、CCSprite、CCMenu等。

2.种类

定时器更新的方式分为三类:

(1)默认定时器  :scheduleUpdate();

(2)自定义定时器:schedule();

(3)一次性定时器:scheduleOnce();

3.Demo下载

https://github.com/shahdza/Cocos_LearningTest/tree/master/demo_%E5%AE%9A%E6%97%B6%E5%99%A8schedule%E3%80%81update

4.scheduleUpdate

默认定时器:scheduleUpdate()。

该定时器默认刷新次数与屏幕刷新频率有关。如频率为60帧每秒,那么scheduleUpdate每秒执行60次刷新。

与scheduleUpdate其对应的刷新函数体为update(),即每一帧会执行一次update()函数。

相关操作如下:

 //
//开启默认定时器。刷新间隔为一帧。
void scheduleUpdate();
void scheduleUpdateWithPriority(int priority); //给予优先级priority。priority越小,优先级越高 virtual void update(float delta); //update为scheduleUpdate定时器的刷新函数体.
//

5.schedule

自定义定时器:schedule()。

该定时器可以自定义指定的刷新函数体、刷新函数体的次数、刷新频率、以及开始刷新的时间。

函数体不一定为update(),可以自己定义。

相关操作如下:

  //
//设置自定义定时器。默认刷新间隔为一帧。
// interval : 每隔interval秒,执行一次。
// repeat : 重复次数。
// delay : 延迟时间,即创建定时器delay秒后开始执行刷新。
//schedule( schedule_selector(HelloWorld::myUpdate), 1.0/60.0 );
void schedule(SEL_SCHEDULE selector); //默认刷新间隔为一帧
void schedule(SEL_SCHEDULE selector, float interval); //自定义刷新间隔,单位:秒
void schedule(SEL_SCHEDULE selector, float interval, unsigned int repeat, float delay);
//

6.scheduleOnce

一次性定时器:scheduleOnce()。

该定时器在等待delay秒延迟时间后,只执行一次刷新函数体,之后就不再刷新。

相关操作如下:

  //
//只执行一次,delay秒后执行
//scheduleOnce( schedule_selector(HelloWorld::myUpdate), 5.0 );
void scheduleOnce(SEL_SCHEDULE selector, float delay);
//

7.其他操作

定时器的取消、暂停、恢复。

相关操作如下:

 //
//this->unscheduleUpdate();
//sprite->unscheduleAllSelectors();
void unscheduleUpdate(void); //取消默认定时器
void unschedule(SEL_SCHEDULE selector); //取消自定义函数的定时器
void unscheduleAllSelectors(void); //取消所有定时器
void pauseSchedulerAndActions(void); //暂停所有定时器和动作
void resumeSchedulerAndActions(void); //恢复所有定时器和动作
//

代码实战(这是2.x的版本格式,可以自己改为3.x的版本格式)

1、在HelloWorld::init()中创建五个精灵

精灵和五种定义定时器的方法,一一对应。

  //
//创建五个精灵
CCSprite* sp = CCSprite::create("Icon.png");
sp->setPosition( ccp(, mysize.height - ) );
this->addChild(sp, , ); //tag标记100 CCSprite* sp1 = CCSprite::create("Icon.png");
sp1->setPosition( ccp(, mysize.height - ) );
this->addChild(sp1, , ); //tag标记101 CCSprite* sp2 = CCSprite::create("Icon.png");
sp2->setPosition( ccp(, mysize.height - ) );
this->addChild(sp2, , ); //tag标记102 CCSprite* sp3 = CCSprite::create("Icon.png");
sp3->setPosition( ccp(, mysize.height - ) );
this->addChild(sp3, , ); //tag标记103 CCSprite* sp4 = CCSprite::create("Icon.png");
sp4->setPosition( ccp(, mysize.height - ) );
this->addChild(sp4, , ); //tag标记104 //定义五个定时器,更新精灵
this->scheduleUpdate();
this->schedule( schedule_selector(HelloWorld::myupdate) );
this->schedule( schedule_selector(HelloWorld::myupdate2), 1.0f );
this->schedule( schedule_selector(HelloWorld::myupdate3), 1.0f, , 3.0f);
this->scheduleOnce( schedule_selector(HelloWorld::myupdate4), 5.0f );
//

2.编写定时器对应的刷新函数体

 //
//scheduleUpdate
void HelloWorld::update(float dt)
{
CCSprite* sp = (CCSprite*)this->getChildByTag(); //获取 tag=100 的精灵
sp->setPosition( sp->getPosition() + ccp(,) ); //每帧移动1
} //schedule(schedule_selector)
void HelloWorld::myupdate(float dt)
{
CCSprite* sp1 = (CCSprite*)this->getChildByTag(); //获取 tag=101 的精灵
sp1->setPosition( sp1->getPosition() + ccp(,) ); //每帧移动1
} //schedule(schedule_selector, interval)
void HelloWorld::myupdate2(float dt)
{
CCSprite* sp2 = (CCSprite*)this->getChildByTag(); //获取 tag=102 的精灵
sp2->setPosition( sp2->getPosition() + ccp(,) ); //每秒移动60
} //schedule(schedule_selector, interval, repeat, delay)
void HelloWorld::myupdate3(float dt)
{
CCSprite* sp3 = (CCSprite*)this->getChildByTag(); //获取 tag=103 的精灵
sp3->setPosition( sp3->getPosition() + ccp(,) ); //每秒移动60
} //scheduleOnce
void HelloWorld::myupdate4(float dt)
{
CCSprite* sp4 = (CCSprite*)this->getChildByTag(); //获取 tag=104 的精灵
sp4->setPosition( sp4->getPosition() + ccp(,) ); //移动100
}
//

3.运行结果

 

4.分析和总结

(1)scheduleUpdate()和schedule(schedule_selector)的效果一样,只是schedule可以自定义刷新函数体,不一定是update()。而scheduleUpdate()的刷新函数体只能为update()。

(2)schedule(schedule_selector, interval):设置了interval=1.0,所以每隔1.0秒执行了一次myupdate2()。

(3)schedule(schedule_selector, interval, repeat, delay):在开始3.0f秒后才开始执行myupdate3(),并且之后又重复执行了5次,就停止更新了。

(4)scheduleOnce(schedule_selector):在开始5秒后,只执行了一次myupdate4(),就停止更新了。

本文出自 “夏天的风” 博客 http://shahdza.blog.51cto.com/2410787/1542014

关于Cocos2d-x中定时器的使用总结的更多相关文章

  1. Unity3D中定时器的使用

    源地址:http://unity3d.9tech.cn/news/2014/0402/40149.html 在游戏设计过程中定时器是必不可少的工具,我们知道update方法是MonoBehavior中 ...

  2. 如何在cocos2d项目中enable ARC

    如何在cocos2d项目中enable ARC 基本思想就是不支持ARC的代码用和支持ARC的分开,通过xcode中设置编译选项,让支持和不支持ARC的代码共存. cocos2d是ios app开发中 ...

  3. 如何在Cocos2D游戏中实现A*寻路算法(六)

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交流 ...

  4. 如何在Cocos2D游戏中实现A*寻路算法(一)

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交流 ...

  5. WPF中定时器与进度条的配合使用

    WPF中定时器使用的注意事项: WPF需要使用System.Windows.Threading.DispatcherTimer定时器,而不能使用System.Timers.Timer定时器.因为Sys ...

  6. [转]Qt中定时器使用的两种方法

    Qt中定时器的使用有两种方法,一种是使用QObject类提供的定时器,还有一种就是使用QTimer类. 其精确度一般依赖于操作系统和硬件,但一般支持20ms.下面将分别介绍两种方法来使用定时器. 方法 ...

  7. JS中定时器的返回数值ID值

    定时器会返回一个数字值id,可以由clearInterval(id)或clearTimeout(id)来实现对对应定时器的清除. setInterval()/setTimeout()BOM中的Wind ...

  8. Java中定时器Timer致命缺点(附学习方法)

    简介 这篇文章我一直在纠结到底要不要写,不想写一来因为定时器用法比较简单,二来是面试中也不常问.后来还是决定写了主要是想把自己分析问题思路分享给大家,让大家在学习过程中能够参考,学习态度我相信大部分人 ...

  9. Javascript中定时器的使用方法

    Javascript中定时器的使用方法 1.间隔定时器(每隔一段时间执行一次代码) 格式:setInterval(函数,时间) //时间单位是毫秒,每隔设置的时间执行函数里的内容一遍(一直执行) // ...

  10. SpringMVC中定时器继承Task后无法对service注入问题

    最近在做一个Spring+MyBatis的一个项目,其中用到了Redis的存储,然后遇到问题是这样的: RedisTask是通过定时器来每分钟像数据库里推送的,于是就有了 public class R ...

随机推荐

  1. PO_本地一揽子采购协议(流程)

    2014-06-04 Created By BaoXinjian

  2. Js 常用函数【持续更新】

    Js Math对象方法介绍:http://www.w3school.com.cn/jsref/jsref_obj_math.asp 1. 算数函数(Math) 1)Js小数取整 常用于:分页算法 js ...

  3. mysql 添加列的索引

    无论哪种模式加入索引.会大幅度增加SELECT速度 索引名:Index_User_Name 栏目名:user_name 索引类型:Nornal 索引方式:BTREE

  4. python标准库介绍——9 copy模块详解

    ==copy 模块== ``copy`` 模块包含两个函数, 用来拷贝对象, 如 [Example 1-64 #eg-1-64] 所示. ``copy(object) => object`` 创 ...

  5. linux shell 命令获取字符串/文件的MD5值

    获取字符串的MD5值: 字符串“hello”的MD5: $ echo -n 'hello'|md5sum|cut -d ' ' -f1 得到的MD5值: 5d41402abc4b2a76b9719d9 ...

  6. shu_1232 老王赛马

    http://202.121.199.212/JudgeOnline/problem.php?cid=1078&pid=2 分析:贪心. 用我方最好的马去解决可以解决的对方的最好的马,如是才干 ...

  7. eclipse中的项目受svn管理

    1.我们在启动Eclipse的时候都会有例如以下图提示: 假设我们直接这样输入目录的名字,这个文件会在eclipse安装目录的同一级自己主动生成这样一个名字叫做njgzw的目录.接下来我们每次启动都用 ...

  8. vue2的全局变量的设置

    最近在学习VUE.js 中间涉及到JS全局变量,与其说是VUE的全局变量,不如说是模块化JS开发的全局变量. 1.全局变量专用模块 就是以一个特定模块来组织管理这些全局量,需要引用的地方导入该模块便好 ...

  9. Android AlarmManager的一些问题

    我开始的代码是这样写的 alarmManager.set(AlarmManager.RTC_WAKEUP, (5*1000), sender); 我的本意是设定五秒后启动闹钟 但是每次都是我设置完闹钟 ...

  10. LeetCode: Evaluate Reverse Polish Notation 解题报告

    Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...