【Cocos2d-x3.0学习笔记 11】Schedule的使用01
2014-11-17 22:48
417 查看
1.Schedule是一个定时器,是单线程的定时器
2.scheduleUpdate和update
scheduleUpdate函数是为了把当前节点添加到队列里,只要把这个节点添加到队列里,那么这个节点就会在游戏的每一帧调用一次update函数。update的float dt参数,是上一次调用到这一次间隔的时间
3.schedule和回调函数
不使用update函数,使用自己的函数void myUpdate(float dt);
//指定每一帧都执行的函数
schedule(schedule_selector(HelloWorld::myUpdate));
//指定间隔时间的定时器,每隔2s执行一次
schedule(schedule_selector(HelloWorld::myUpdate),2.0f);
4.取消定时器
取消默认的update函数调用:unscheduleUpdate();
取消自定义的函数调用
unschedule(schedule_selector(HelloWorld::myUpdate));
取消所有的update函数
unscheduleAllSelectors();
5.触发器
//2s后执行myUpdate函数,只执行一次scheduleOnce(schedule_selector(HelloWorld::myUpdate),2.0f);
6.不精确的时间
不管是schedule还是scheduleOnce,都有可能出现时间差,因为我们无法预计在某段时间内是否会有耗时的操作发生7.更准确的计时
可以根据当前时间的总和来判断时间TimeCounter #include "cocos2d" USING_NS_CC; class TimerCounter : public Node{ public: CREATE_FUN(TimerCounter); virtual bool init(); virtual void update(float dt); void start();//开始计时 float getCountTime();//获取当前时间 private: float m_time; } #include "TimeCounter.h" bool TimeCounter::init(){ return true; } void TimeCounter::update(float dt){ m_time += dt; } float TimeCounter::getCountTime(){ return m_time; } void TimeCounter::start(){ m_time = 0; this->scheduleUpdate(); }
8.定时回调功能封装类
到指定时间调用回调函数TimeCounter #include "cocos2d" USING_NS_CC; class TimerCounter : public Node{ public: CREATE_FUN(TimerCounter); virtual bool init(); virtual void update(float dt); void start(float callBackTime,std::function<void()> func);//开始计时 //指定回调时间和回调函数 private: float m_time; float m_callBackTime;//回调时间 bool m_isCounting;//是否正在计时 std:function<void()> m_func;//回调函数 } #include "TimeCounter.h" bool TimeCounter::init(){ m_isCounting = false; this->scheduleUpdate(); return true; } void TimeCounter::update(float dt){ if(!m_isCounting){ return; } m_time += dt; if(m_time >= m_callBackTime){ m_func(); m_isCounting = false; } } void TimeCounter::start(float callBackTime,std::function<void()> func){ m_time = 0; m_callBackTime = callBackTime; m_func = func; m_isCounting = true; }
相关文章推荐
- 【Cocos2d-x3.0学习笔记 09】游戏实例跑跑跑01
- 【Cocos2d-x3.0学习笔记 01】熟悉的环境搭建
- 【Cocos2d-x3.0学习笔记 03】一些基本的使用
- cocos2d-x学习笔记07:在cocos2d-x中使用RTTI
- cocos2d-x学习笔记01:VS开发环境搭建
- cocos2d-x学习笔记01:VS开发环境搭建
- cocos2d-x学习笔记17:记录存储2:SQLite基本使用
- 【Android】学习笔记(11)——Animation使用方法(上)
- cocos2d-x学习笔记(15)--地图的使用1(TMX)
- cocos2d-x学习笔记07:在cocos2d-x中使用RTTI
- cocos2d-x学习笔记(22)--地图的使用2(TMX) --Z-Order、AnchorPoin、抗锯齿
- cocos2d-x学习笔记01:VS开发环境搭建
- Flex学习笔记_05 使用组件处理数据和交互_01常用组件
- cocos2d-x学习笔记17:记录存储2:SQLite基本使用 推荐
- cocos2d-x学习笔记11:xcode下编译运行helloworld
- 《Javascript高级程序设计》(第2版)学习笔记01--在HTML中使用
- cocos2d-x学习笔记11:xcode下编译运行helloworld
- cocos2d-x学习笔记番外篇04:开发问题汇总01 推荐
- cocos2d-x学习笔记19:记录存储3:使用摘要算法进行存档校验 推荐
- cocos2d-x学习笔记18:内存管理01:概述