修复Action在拷贝和逆向拷贝时导致错误的BUG

This commit is contained in:
Nomango 2017-09-30 16:18:45 +08:00
parent c365364255
commit 5d51e7143d
16 changed files with 104 additions and 74 deletions

View File

@ -8,6 +8,8 @@ Action::Action() :
{
// 默认动作 15ms 运行一次
setInterval(15);
// 保留动作
this->retain();
}
Action::~Action()

View File

@ -9,7 +9,7 @@ ActionCallback::~ActionCallback()
{
}
ActionCallback * ActionCallback::copy()
ActionCallback * ActionCallback::copy() const
{
return new ActionCallback(m_Callback);
}

View File

@ -9,9 +9,9 @@ ActionDelay::~ActionDelay()
{
}
ActionDelay * easy2d::ActionDelay::copy()
ActionDelay * ActionDelay::copy() const
{
return new ActionDelay(*this);
return new ActionDelay(m_nMilliSeconds / 1000.0f);
}
void ActionDelay::_init()

View File

@ -7,6 +7,12 @@ ActionFrames::ActionFrames() :
setInterval(500);
}
ActionFrames::ActionFrames(UINT frameDelay) :
m_nFrameIndex(0)
{
setInterval(frameDelay);
}
ActionFrames::~ActionFrames()
{
for (auto frame : m_vFrames)
@ -33,6 +39,7 @@ bool ActionFrames::_exec(LARGE_INTEGER nNow)
m_nLast.QuadPart = nNow.QuadPart - (nNow.QuadPart % m_nAnimationInterval.QuadPart);
m_pParent->setImage(m_vFrames[m_nFrameIndex]);
m_nFrameIndex++;
// 判断动作是否结束
if (m_nFrameIndex == m_vFrames.size())
{
return true;
@ -55,9 +62,9 @@ void ActionFrames::addFrame(Image * frame)
}
}
ActionFrames * ActionFrames::copy()
ActionFrames * ActionFrames::copy() const
{
auto a = new ActionFrames();
auto a = new ActionFrames(this->m_nMilliSeconds);
for (auto f : m_vFrames)
{
a->addFrame(f);
@ -67,8 +74,7 @@ ActionFrames * ActionFrames::copy()
ActionFrames * ActionFrames::reverse() const
{
auto a = new ActionFrames();
a->m_vFrames = this->m_vFrames;
auto a = this->copy();
a->m_vFrames.reserve(m_vFrames.size());
return a;
}

View File

@ -29,7 +29,7 @@ bool ActionMoveBy::_exec(LARGE_INTEGER nNow)
m_pParent->setPos(int(m_BeginPos.x + m_MoveVector.x * scale),
int(m_BeginPos.y + m_MoveVector.y * scale));
// 判断动作是否结束
if (m_nDuration >= m_nTotalDuration)
if (_isEnd())
{
return true;
}
@ -42,11 +42,9 @@ void ActionMoveBy::_reset()
Animation::_reset();
}
ActionMoveBy * ActionMoveBy::copy()
ActionMoveBy * ActionMoveBy::copy() const
{
auto a = new ActionMoveBy(*this);
a->_reset();
return a;
return new ActionMoveBy(m_nMilliSeconds / 1000.0f, m_MoveVector);
}
ActionMoveBy * ActionMoveBy::reverse() const

View File

@ -10,11 +10,9 @@ ActionMoveTo::~ActionMoveTo()
{
}
ActionMoveTo * ActionMoveTo::copy()
ActionMoveTo * ActionMoveTo::copy() const
{
auto a = new ActionMoveTo(*this);
a->_reset();
return a;
return new ActionMoveTo(m_nMilliSeconds / 1000.0f, m_EndPos);
}
void ActionMoveTo::_init()

View File

@ -11,7 +11,7 @@ ActionNeverStop::~ActionNeverStop()
SAFE_RELEASE(m_Action);
}
ActionNeverStop * ActionNeverStop::copy()
ActionNeverStop * ActionNeverStop::copy() const
{
return new ActionNeverStop(m_Action->copy());
}

View File

@ -28,7 +28,7 @@ bool ActionOpacityBy::_exec(LARGE_INTEGER nNow)
// 移动 Sprite
m_pParent->setOpacity(m_nBeginVal + m_nVariation * scale);
// 判断动作是否结束
if (m_nDuration >= m_nTotalDuration)
if (_isEnd())
{
return true;
}
@ -41,11 +41,9 @@ void ActionOpacityBy::_reset()
Animation::_reset();
}
ActionOpacityBy * ActionOpacityBy::copy()
ActionOpacityBy * ActionOpacityBy::copy() const
{
auto a = new ActionOpacityBy(*this);
a->_reset();
return a;
return new ActionOpacityBy(m_nMilliSeconds / 1000.0f, m_nVariation);
}
ActionOpacityBy * ActionOpacityBy::reverse() const

View File

@ -10,11 +10,9 @@ ActionOpacityTo::~ActionOpacityTo()
{
}
ActionOpacityTo * ActionOpacityTo::copy()
ActionOpacityTo * ActionOpacityTo::copy() const
{
auto a = new ActionOpacityTo(*this);
a->_reset();
return a;
return new ActionOpacityTo(m_nMilliSeconds / 1000.0f, m_nEndVal);
}
void ActionOpacityTo::_init()

View File

@ -30,7 +30,7 @@ bool ActionScaleBy::_exec(LARGE_INTEGER nNow)
// 移动 Sprite
m_pParent->setScale(m_nBeginScaleX + m_nVariationX * scale, m_nBeginScaleX + m_nVariationX * scale);
// 判断动作是否结束
if (m_nDuration >= m_nTotalDuration)
if (_isEnd())
{
return true;
}
@ -43,11 +43,9 @@ void ActionScaleBy::_reset()
Animation::_reset();
}
ActionScaleBy * ActionScaleBy::copy()
ActionScaleBy * ActionScaleBy::copy() const
{
auto a = new ActionScaleBy(*this);
a->_reset();
return a;
return new ActionScaleBy(m_nMilliSeconds / 1000.0f, m_nVariationX, m_nVariationY);
}
ActionScaleBy * ActionScaleBy::reverse() const

View File

@ -11,11 +11,9 @@ ActionScaleTo::~ActionScaleTo()
{
}
ActionScaleTo * ActionScaleTo::copy()
ActionScaleTo * ActionScaleTo::copy() const
{
auto a = new ActionScaleTo(*this);
a->_reset();
return a;
return new ActionScaleTo(m_nMilliSeconds / 1000.0f, m_nEndScaleX, m_nEndScaleY);
}
void ActionScaleTo::_init()

View File

@ -1,6 +1,11 @@
#include "..\easy2d.h"
#include <stdarg.h>
ActionSequence::ActionSequence() :
m_nActionIndex(0)
{
}
ActionSequence::ActionSequence(int number, Action * action1, ...) :
m_nActionIndex(0)
{
@ -9,9 +14,7 @@ ActionSequence::ActionSequence(int number, Action * action1, ...) :
while (number > 0)
{
Action* arg = va_arg(params, Action*);
arg->retain();
m_vActions.push_back(arg);
this->addAction(va_arg(params, Action*));
number--;
}
@ -64,26 +67,37 @@ void ActionSequence::_reset()
m_nActionIndex = 0;
}
ActionSequence * ActionSequence::copy()
void ActionSequence::addAction(Action * action)
{
auto a = new ActionSequence(*this);
a->_reset();
m_vActions.push_back(action);
action->retain();
}
ActionSequence * ActionSequence::copy() const
{
auto a = new ActionSequence();
for (auto action : m_vActions)
{
a->addAction(action->copy());
}
return a;
}
ActionSequence * ActionSequence::reverse() const
ActionSequence * ActionSequence::reverse(bool actionReverse) const
{
// 复制一个相同的动作
auto a = new ActionSequence(*this);
a->_reset();
a->m_bRunning = true;
a->m_bStop = false;
// 将动作顺序逆序排列
a->m_vActions.reserve(m_vActions.size());
// 将所有动作逆向运行
auto a = new ActionSequence();
for (auto action : a->m_vActions)
{
action->reverse();
if (actionReverse)
{
a->addAction(action->reverse());
}
else
{
a->addAction(action->copy());
}
}
// ½«¶¯×÷˳ÐòÄæÐòÅÅÁÐ
a->m_vActions.reserve(m_vActions.size());
return a;
}

View File

@ -14,16 +14,21 @@ ActionTwo::~ActionTwo()
SAFE_RELEASE(m_SecondAction);
}
ActionTwo * ActionTwo::copy()
ActionTwo * ActionTwo::copy() const
{
auto a = new ActionTwo(*this);
a->_reset();
return a;
return new ActionTwo(m_FirstAction->copy(), m_SecondAction->copy());
}
ActionTwo * ActionTwo::reverse() const
ActionTwo * ActionTwo::reverse(bool actionReverse) const
{
return new ActionTwo(m_SecondAction->copy(), m_FirstAction->copy());
if (actionReverse)
{
return new ActionTwo(m_SecondAction->reverse(), m_FirstAction->reverse());
}
else
{
return new ActionTwo(m_SecondAction->copy(), m_FirstAction->copy());
}
}
void ActionTwo::_init()

View File

@ -10,6 +10,11 @@ Animation::~Animation()
{
}
bool Animation::_isEnd() const
{
return m_nDuration >= m_nTotalDuration;
}
void Animation::_init()
{
// 记录当前时间

View File

@ -1,4 +1,5 @@
#include "..\easy2d.h"
#include <assert.h>
static std::vector<Action*> s_vActions;
@ -28,9 +29,14 @@ void ActionManager::addAction(Action * action)
{
if (action)
{
action->_init();
#ifdef _DEBUG
for (auto a : s_vActions)
{
assert(a != action);
}
#endif
s_vActions.push_back(action);
action->retain();
action->_init();
}
}

View File

@ -1044,7 +1044,7 @@ public:
void pause();
void stop();
void setInterval(UINT ms);
virtual Action * copy() = 0;
virtual Action * copy() const = 0;
virtual Action * reverse() const;
protected:
@ -1073,6 +1073,7 @@ protected:
UINT m_nTotalDuration;
protected:
bool _isEnd() const;
virtual void _init() override;
virtual bool _exec(LARGE_INTEGER nNow) override;
virtual void _reset() override;
@ -1085,7 +1086,7 @@ public:
ActionMoveBy(float duration, CVector vec);
virtual ~ActionMoveBy();
virtual ActionMoveBy * copy() override;
virtual ActionMoveBy * copy() const override;
virtual ActionMoveBy * reverse() const override;
protected:
@ -1105,7 +1106,7 @@ public:
ActionMoveTo(float duration, CPoint pos);
virtual ~ActionMoveTo();
virtual ActionMoveTo * copy() override;
virtual ActionMoveTo * copy() const override;
protected:
CPoint m_EndPos;
@ -1122,7 +1123,7 @@ public:
ActionScaleBy(float duration, float scaleX, float scaleY);
virtual ~ActionScaleBy();
virtual ActionScaleBy * copy() override;
virtual ActionScaleBy * copy() const override;
virtual ActionScaleBy * reverse() const override;
protected:
@ -1144,7 +1145,7 @@ public:
ActionScaleTo(float duration, float scaleX, float scaleY);
virtual ~ActionScaleTo();
virtual ActionScaleTo * copy() override;
virtual ActionScaleTo * copy() const override;
protected:
float m_nEndScaleX;
@ -1162,7 +1163,7 @@ public:
ActionOpacityBy(float duration, float opacity);
virtual ~ActionOpacityBy();
virtual ActionOpacityBy * copy() override;
virtual ActionOpacityBy * copy() const override;
virtual ActionOpacityBy * reverse() const override;
protected:
@ -1182,7 +1183,7 @@ public:
ActionOpacityTo(float duration, float opacity);
virtual ~ActionOpacityTo();
virtual ActionOpacityTo * copy() override;
virtual ActionOpacityTo * copy() const override;
protected:
float m_nEndVal;
@ -1213,8 +1214,8 @@ public:
ActionTwo(Action * actionFirst, Action * actionSecond);
virtual ~ActionTwo();
virtual ActionTwo * copy() override;
virtual ActionTwo * reverse() const override;
virtual ActionTwo * copy() const override;
virtual ActionTwo * reverse(bool actionReverse = true) const;
protected:
Action * m_FirstAction;
@ -1230,11 +1231,13 @@ class ActionSequence :
public Action
{
public:
ActionSequence();
ActionSequence(int number, Action * action1, ...);
virtual ~ActionSequence();
virtual ActionSequence * copy() override;
virtual ActionSequence * reverse() const override;
void addAction(Action * action);
virtual ActionSequence * copy() const override;
virtual ActionSequence * reverse(bool actionReverse = true) const;
protected:
UINT m_nActionIndex;
@ -1253,7 +1256,7 @@ public:
ActionDelay(float duration);
virtual ~ActionDelay();
virtual ActionDelay * copy() override;
virtual ActionDelay * copy() const override;
protected:
virtual void _init() override;
@ -1268,7 +1271,7 @@ public:
ActionNeverStop(Action * action);
virtual ~ActionNeverStop();
virtual ActionNeverStop * copy() override;
virtual ActionNeverStop * copy() const override;
protected:
Action * m_Action;
@ -1284,10 +1287,11 @@ class ActionFrames :
{
public:
ActionFrames();
ActionFrames(UINT frameDelay);
~ActionFrames();
void addFrame(Image * frame);
virtual ActionFrames * copy() override;
virtual ActionFrames * copy() const override;
virtual ActionFrames * reverse() const override;
protected:
@ -1307,7 +1311,7 @@ public:
ActionCallback(const std::function<void()>& callback);
~ActionCallback();
virtual ActionCallback * copy() override;
virtual ActionCallback * copy() const override;
protected:
std::function<void()> m_Callback;