加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

cocos2dx 2.x定时器分析(4)

发布时间:2020-12-14 20:43:28 所属栏目:百科 来源:网络整理
导读:脚本定时器 1、添加脚本定时器 /** The scheduled script callback will be called every 'interval' seconds. If paused is YES,then it won't be called until it is resumed. If 'interval' is 0,it will be called every frame. return schedule script

脚本定时器

1、添加脚本定时器
    /** The scheduled script callback will be called every 'interval' seconds.
     If paused is YES,then it won't be called until it is resumed.
     If 'interval' is 0,it will be called every frame.
     return schedule script entry ID,used for unscheduleScriptFunc().
     @js NA
     */
    //供脚本lua使用的定时器函数
    unsigned int scheduleScriptFunc(unsigned int nHandler,float fInterval,bool bPaused);

    -->>源码:
    unsigned int CCScheduler::scheduleScriptFunc(unsigned int nHandler,bool bPaused)
{
    //CCSchedulerScriptHandlerEntry类在CCScriptSupport中
    CCSchedulerScriptHandlerEntry* pEntry = CCSchedulerScriptHandlerEntry::create(nHandler,fInterval,bPaused);
    if (!m_pScriptHandlerEntries)
    {
        m_pScriptHandlerEntries = CCArray::createWithCapacity(20);
        m_pScriptHandlerEntries->retain();
    }

    //CCArray* m_pScriptHandlerEntries,脚本定时器的数组
    m_pScriptHandlerEntries->addObject(pEntry);
    return pEntry->getEntryId();
}


2、移除脚本定时器
    /** Unschedule a script entry. 
     *  @js NA
     */
    void unscheduleScriptEntry(unsigned int uScheduleScriptEntryID);

-->>
    void CCScheduler::unscheduleScriptEntry(unsigned int uScheduleScriptEntryID)
{
    for (int i = m_pScriptHandlerEntries->count() - 1; i >= 0; i--)
    {
        CCSchedulerScriptHandlerEntry* pEntry = static_cast<CCSchedulerScriptHandlerEntry*>(m_pScriptHandlerEntries->objectAtIndex(i));
        if (pEntry->getEntryId() == (int)uScheduleScriptEntryID)
        {   //只是设置了移除标志,并没有真正移除,真正的移除在其他地方
            pEntry->markedForDeletion();
            break;
        }
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读