Cocos2d-x 每一帧执行事件之 scheduleUpdate()与Update()以及schedule()定时刷新的使用
来源:互联网 发布:1hhhhh域名访问升级 编辑:程序博客网 时间:2024/05/01 10:18
在Unity中有OnGUI、Update等每一帧都调用的函数,我们把一些逻辑以及界面更新写在这些函数里面。
顺手在Cocos2d-x中就Virtual void Update(float dt),然后实现,但是断点却没有进去!
class CocosRakNet :public TristanaLayer{public:staticCCScene* scene();virtual bool init();void update(float delta);
void CocosRakNet::update(float delta){int i=0;}
原来在Cocos2d-x中的每一帧执行事件,只有Update是不行的,还要在init()函数里面加上scheduleUpdate(),这样才会每一帧都调用update()。
bool CocosRakNet::init(){bool bRet=false;do{CC_BREAK_IF(!TristanaLayer::init());CCSize winSize=CCDirector::sharedDirector()->getWinSize();CCMenu* menu=CCMenu::create();CCMenuItemImage* pMenuItemImage=CCMenuItemImage::create("CloseNormal.png","CloseSelected.png","",this,menu_selector(CocosRakNet::SendMessageCallBack));pMenuItemImage->setPosition(ccp(winSize.width/2,winSize.height/2));menu->addChild(pMenuItemImage);CCMenuItemImage* pRequestRoleInfoMenuItemImage=CCMenuItemImage::create("CloseNormal.png","CloseSelected.png","",this,menu_selector(CocosRakNet::RequestRoleInfo));pRequestRoleInfoMenuItemImage->setPosition(ccp(200,200));menu->addChild(pRequestRoleInfoMenuItemImage);menu->setPosition(CCPointZero);addChild(menu);//更新函数scheduleUpdate();bRet=true;} while (0);return bRet;}
那么 schedule() 这个函数呢?。
Schedule() 函数有两种方式,一种带时间参数,一种不带时间参数。
带时间参数的,间隔指定时间,然后执行指定的函数,达到定时刷新的效果。
不带时间参数的,每一帧刷新,执行指定的函数,作用效果就和scheduleUpdate() 函数差不多效果,不过函数可以自己指定。
不带时间参数的写法(写在init()中),这样每一帧执行update() 函数
schedule(schedule_selector(CocosRakNet::update));
带时间参数的写法,每隔指定时间执行update() 函数
schedule(schedule_selector(CocosRakNet::update),3.0f);
注意这里的时间间隔是 秒 为单位
来看schedule()函数的参数解释:
void schedule(SEL_SCHEDULE selector, float interval, unsigned int repeat, float delay);
/** * Schedules a custom selector. * * If the selector is already scheduled, then the interval parameter will be updated without scheduling it again. * @code * // firstly, implement a schedule function * void MyNode::TickMe(float dt); * // wrap this function into a selector via schedule_selector marco. * this->schedule(schedule_selector(MyNode::TickMe), 0, 0, 0); * @endcode * * @param interval Tick interval in seconds. 0 means tick every frame. If interval = 0, it's recommended to use scheduleUpdate() instead. * @param repeat The selector will be excuted (repeat + 1) times, you can use kCCRepeatForever for tick infinitely. * @param delay The amount of time that the first tick will wait before execution. */
这个函数用来执行一个计划任务。PS:百度的翻译。。
interval:如果这个参数是0,那么每一帧都会执行指定函数,这样的话,建议使用scheduleUpdate() 函数
repeat:指定函数将会执行指定次数。
delay:函数执行前,将会有delay时间的延迟。
如何让只执行一次函数?
this->scheduleOnce(schedule_selector(HelloWorld::Update), 5.0f);
这样就会延迟5秒之后执行一次Update,而且只执行一次!
那如何停止schedule ?
1.停止scheduleUpdate()关联的update()函数
unscheduleUpdate();
2.停止schedule() 关联的自己的函数
schedule(schedule_selector(CocosRakNet::update),3.0f,10,5.0f);unschedule(schedule_selector(CocosRakNet::update));
3. 停止所有的schedule,不管是自己的还是默认关联的
//更新函数scheduleUpdate();schedule(schedule_selector(CocosRakNet::update));schedule(schedule_selector(CocosRakNet::update),3.0f);unschedule(schedule_selector(CocosRakNet::update));unscheduleAllSelectors();
- Cocos2d-x 每一帧执行事件之 scheduleUpdate()与Update()以及schedule()定时刷新的使用
- Cocos2d-x 每一帧执行事件之 scheduleUpdate()与Update()以及
- Cocos2d-x 每一帧执行事件之 scheduleUpdate()与Update()以及
- Cocos2D-x schedule & scheduleUpdate 的使用
- Cocos2D-x schedule & scheduleUpdate 的使用
- 【爱上cocos2d-x之十四】每一帧都调用,scheduleUpdate和update
- cocos2d-x调用scheduleUpdate()不执行update()方法的解决办法
- cocos2d-x调用scheduleUpdate()不执行update()方法的解决办法
- cocos2d-x调用scheduleUpdate()不执行update()方法的解决办法
- cocos2d-x调用scheduleUpdate()不执行update()方法的解决办法
- cocos2d-x调用scheduleUpdate()不执行update()方法的解决办法
- Cocos2d-x三种定时器的使用schedule,scheduleUpdate,scheduleOnce
- 我的Cocos2d-x学习笔记(十)定时调度器(scheduleUpdate、scheduleOnce、schedule)
- cocos2d-js:调用scheduleUpdate()不执行update()方法的解决办法
- Cocos2dx之定时器schedule,scheduleUpdate,scheduleOnce的使用
- [Cocos2d-x]schedule单独使用无法执行的问题
- cocos2d开发之定时消息(scheduleUpdate)
- cocos2d-x 定时器update,schedule
- synchronized 类型方法妙用
- springMVC入门(二)
- 加密算法中的加盐值
- Win7与debian6搭建双系统(手动修复启动项)
- 使用和众达仿真器CCS3.3.8环境调试出现tconf.exe不能工作问题解决方法
- Cocos2d-x 每一帧执行事件之 scheduleUpdate()与Update()以及schedule()定时刷新的使用
- VC2008解决方案设置的一些有用方法
- 超级硬盘数据恢复软件 4.2.1.0注册码破解版
- Struts 1 中如何测试Action
- android获取相册图片 拍照获取图片 用到的东西
- 常见的动态规划问题分析与求解
- 二叉树的非递归遍历
- Android系统的改进(四)-- 短信直接加入黑名单功能
- java调用百度地图web服务api-----该方法可用在js跨域请求上