mirror of https://github.com/axmolengine/axmol.git
142 lines
3.6 KiB
C++
142 lines
3.6 KiB
C++
/****************************************************************************
|
|
Copyright (c) 2010-2011 cocos2d-x.org
|
|
Copyright (c) 2008-2010 Ricardo Quesada
|
|
Copyright (c) 2011 Zynga Inc.
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
in the Software without restriction, including without limitation the rights
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
all copies or substantial portions of the Software.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
THE SOFTWARE.
|
|
****************************************************************************/
|
|
|
|
#include "CCTimer.h"
|
|
#if LUA_ENGINE
|
|
#include "CCLuaEngine.h"
|
|
#endif
|
|
|
|
namespace cocos2d
|
|
{
|
|
|
|
CCTimer::CCTimer()
|
|
: m_pTarget(NULL)
|
|
, m_functionRefID(0)
|
|
, m_fInterval(0.0f)
|
|
, m_fElapsed(0.0f)
|
|
, m_pfnSelector(NULL)
|
|
{
|
|
}
|
|
|
|
CCTimer::~CCTimer()
|
|
{
|
|
#if LUA_ENGINE
|
|
if (m_functionRefID)
|
|
{
|
|
CCLuaEngine::sharedEngine()->removeLuaFunctionRef(m_functionRefID);
|
|
}
|
|
#endif
|
|
}
|
|
|
|
CCTimer* CCTimer::timerWithTarget(SelectorProtocol *pTarget, SEL_SCHEDULE pfnSelector)
|
|
{
|
|
CCTimer *pTimer = new CCTimer();
|
|
|
|
pTimer->initWithTarget(pTarget, pfnSelector);
|
|
pTimer->autorelease();
|
|
|
|
return pTimer;
|
|
}
|
|
|
|
CCTimer* CCTimer::timerWithScriptFunc(int refid, ccTime fSeconds)
|
|
{
|
|
CCTimer *pTimer = new CCTimer();
|
|
|
|
pTimer->initWithScriptFunc(refid, fSeconds);
|
|
pTimer->autorelease();
|
|
|
|
return pTimer;
|
|
}
|
|
|
|
CCTimer* CCTimer::timerWithTarget(SelectorProtocol *pTarget, SEL_SCHEDULE pfnSelector, ccTime fSeconds)
|
|
{
|
|
CCTimer *pTimer = new CCTimer();
|
|
|
|
pTimer->initWithTarget(pTarget, pfnSelector, fSeconds);
|
|
pTimer->autorelease();
|
|
|
|
return pTimer;
|
|
}
|
|
|
|
#if LUA_ENGINE
|
|
bool CCTimer::initWithScriptFunc(int functionRefID, ccTime fSeconds)
|
|
{
|
|
if (m_functionRefID)
|
|
{
|
|
CCLuaEngine::sharedEngine()->removeLuaFunctionRef(m_functionRefID);
|
|
}
|
|
m_functionRefID = functionRefID;
|
|
m_fInterval = fSeconds;
|
|
m_fElapsed = -1;
|
|
return true;
|
|
}
|
|
#endif
|
|
|
|
bool CCTimer::initWithTarget(SelectorProtocol *pTarget, SEL_SCHEDULE pfnSelector)
|
|
{
|
|
return initWithTarget(pTarget, pfnSelector, 0);
|
|
}
|
|
|
|
bool CCTimer::initWithTarget(SelectorProtocol *pTarget, SEL_SCHEDULE pfnSelector, ccTime fSeconds)
|
|
{
|
|
m_pTarget = pTarget;
|
|
m_pfnSelector = pfnSelector;
|
|
m_fElapsed = -1;
|
|
m_fInterval = fSeconds;
|
|
return true;
|
|
}
|
|
|
|
void CCTimer::update(ccTime dt)
|
|
{
|
|
if (m_fElapsed == -1)
|
|
{
|
|
m_fElapsed = 0;
|
|
}
|
|
else
|
|
{
|
|
m_fElapsed += dt;
|
|
}
|
|
|
|
if (m_fElapsed >= m_fInterval)
|
|
{
|
|
if (m_pfnSelector)
|
|
{
|
|
(m_pTarget->*m_pfnSelector)(m_fElapsed);
|
|
m_fElapsed = 0;
|
|
}
|
|
#if LUA_ENGINE
|
|
if (m_functionRefID)
|
|
{
|
|
CCLuaEngine::sharedEngine()->executeSchedule(m_functionRefID, m_fElapsed);
|
|
m_fElapsed = 0;
|
|
}
|
|
#endif
|
|
}
|
|
}
|
|
|
|
} // namespace cocos2d
|
|
|