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

前端之家收集整理的这篇文章主要介绍了cocos2dx 2.x定时器分析(4)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

脚本定时器

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;
        }
    }
}

猜你在找的Cocos2d-x相关文章