第十五节cocosdx的定时器

来源:互联网 发布:有向图自动布局算法 编辑:程序博客网 时间:2024/06/05 00:04
定时器分析:
CCNode 里面有相关接口,主要为每一帧刷新以及定时刷新:
每一帧刷新:
scheduleupdate();
然后实现:
void XXX::update(float fDelta)
{
CCNode::update(fDelta);
自己的代码
}
定时代码为:
void CCNode::schedule(SEL_SCHEDULE selector)// 一直调用
void CCNode::schedule(SEL_SCHEDULE selector, float interval)// 传入调用间隔
void CCNode::schedule(SEL_SCHEDULE selector, float interval, unsigned int repeat, float
delay)
// 传入调用间隔重复次数以及重复之间的间隔
void CCNode::scheduleOnce(SEL_SCHEDULE selector, float delay)
调用一次函数
其中typedef void (CCObject::*SEL_SCHEDULE)(float);
我们使用#define schedule_selector(_SELECTOR) (SEL_SCHEDULE)(&_SELECTOR)来进行操作。
void CCNode::unschedule(SEL_SCHEDULE selector)
撤掉对应的定时器。
CCNode::unscheduleUpdate();
撤掉每帧更新的定时器。
onenter 里面调用:
resumeSchedulerAndActions();
onexit 里面调用:
pauseSchedulerAndActions();
在更换定时器调度实例时调用:
unscheduleAllSelectors();
接着我们跟踪下schedule 函数。
m_pScheduler->scheduleSelector(selector, this, interval , repeat, delay, !m_bRunning);
里面主要做几件事情:
查找一个存储列表,申请一个CCTimer,存储下时间,回调函数,注册在
ccArrayAppendObject(pElement->timers, pTimer);
这里面。
里面如果回调函数一致,则会修改回调时间,直接就可以返回了。
调度器:
CCScheduler.cpp 里面的update 函数会在CCDirector.cpp 里面的drawScene 里面调用。
drawScene 函数会在CCDisplayLinkDirector::mainLoop(void)里面调用,mainloop 会在每一帧进
行调用。
回到CCScheduler::update(float dt)里面来,df 这个值为时间流逝值
if (m_fTimeScale != 1.0f)
{
dt *= m_fTimeScale;
}
这里的m_fTimeScale 可以实现加速减速。
然后执行代码为
1>更新每帧的update,优先顺序为<0, =0,>0.
2>刷新所有的定时器
for (tHashTimerEntry *elt = m_pHashForTimers; elt != NULL; )
{
m_pCurrentTarget = elt;
m_bCurrentTargetSalvaged = false;
if (! m_pCurrentTarget->paused)
{
// The 'timers' array may change while inside this loop
for (elt->timerIndex = 0; elt->timerIndex < elt->timers->num;
++(elt->timerIndex))
{
elt->currentTimer = (CCTimer*)(elt->timers->arr[elt->timerIndex]);
elt->currentTimerSalvaged = false;
elt->currentTimer->update(dt);
if (elt->currentTimerSalvaged)
{
// The currentTimer told the remove itself. To prevent the timer from
// accidentally deallocating itself before finishing its step, we retained
// it. Now that step is done, it's safe to release it.
elt->currentTimer->release();
}
elt->currentTimer = NULL;
}
}
3>脚本时间回调,我们不需要关注,因为我们开发的是android 版本。
4>回收标记过时的timer
这里我们没有看到action 动作执行过程,那么我们继续跟踪:
在CCDirector::init(void)里面:
m_pActionManager = new CCActionManager();
m_pScheduler->scheduleUpdateForTarget(m_pActionManager, kCCPrioritySystem,
false);
这里可以看到action 动作是注册在Scheduler 里面的,所以之前的
CCScheduler::update(float dt)里面是处理了action 动作执行了。
本节收尾,下节继续分析,跟踪下一个CCActionManager 管理的Action。
原创粉丝点击