Change Tween Type to Frame Index

This commit is contained in:
yinkaile 2013-07-08 23:25:11 +08:00
parent 6fc0eaf5b1
commit 4a8cde4dad
5 changed files with 53 additions and 33 deletions

View File

@ -54,7 +54,6 @@ CCTween::CCTween()
, m_pFrom(NULL) , m_pFrom(NULL)
, m_pTo(NULL) , m_pTo(NULL)
, m_pBetween(NULL) , m_pBetween(NULL)
, m_pCurrentKeyFrame(NULL)
, m_pBone(NULL) , m_pBone(NULL)
, m_eFrameTweenEasing(Linear) , m_eFrameTweenEasing(Linear)
@ -101,11 +100,10 @@ void CCTween::play(CCMovementBoneData *movementBoneData, int durationTo, int dur
m_eLoopType = (AnimationType)loop; m_eLoopType = (AnimationType)loop;
m_pCurrentKeyFrame = NULL;
m_iTotalDuration = 0; m_iTotalDuration = 0;
m_BetweenDuration = 0; m_BetweenDuration = 0;
m_iToIndex = 0; m_iFromIndex = m_iToIndex = 0;
bool difMovement = movementBoneData != m_pMovementBoneData; bool difMovement = movementBoneData != m_pMovementBoneData;
@ -126,7 +124,6 @@ void CCTween::play(CCMovementBoneData *movementBoneData, int durationTo, int dur
} }
m_eFrameTweenEasing = Linear; m_eFrameTweenEasing = Linear;
m_iRawDuration = m_pMovementBoneData->duration; m_iRawDuration = m_pMovementBoneData->duration;
m_iFromIndex = m_iToIndex = 0;
} }
else if (m_pMovementBoneData->frameList.count() > 1) else if (m_pMovementBoneData->frameList.count() > 1)
{ {
@ -217,8 +214,6 @@ void CCTween::updateHandler()
// //
m_fCurrentFrame = (1 - m_pMovementBoneData->delay) * (float)m_iNextFrameIndex; m_fCurrentFrame = (1 - m_pMovementBoneData->delay) * (float)m_iNextFrameIndex;
m_fCurrentPercent = m_fCurrentFrame / m_iNextFrameIndex; m_fCurrentPercent = m_fCurrentFrame / m_iNextFrameIndex;
} }
else else
{ {
@ -266,10 +261,6 @@ void CCTween::updateHandler()
{ {
tweenNodeTo(percent); tweenNodeTo(percent);
} }
else if(m_pCurrentKeyFrame)
{
tweenNodeTo(0);
}
} }
void CCTween::setBetween(CCFrameData *from, CCFrameData *to) void CCTween::setBetween(CCFrameData *from, CCFrameData *to)
@ -328,7 +319,7 @@ void CCTween::arriveKeyFrame(CCFrameData *keyFrameData)
} }
// if(keyFrameData->m_strSound.length() != 0) // if(keyFrameData->m_strSound.length() != 0)
// { // {
// //soundManager.dispatchEventWith(Event.SOUND_FRAME, m_pCurrentKeyFrame->sound); // //soundManager.dispatchEventWith(Event.SOUND_FRAME, keyFrameData->sound);
// } // }
} }
} }
@ -366,26 +357,33 @@ void CCTween::tweenColorTo(float percent, CCFrameData *node, bool dirty)
float CCTween::updateFrameData(float currentPrecent, bool activeFrame) float CCTween::updateFrameData(float currentPrecent, bool activeFrame)
{ {
if (currentPrecent > 1)
{
currentPrecent = fmodf(currentPrecent,1);
}
float playedTime = (float)m_iRawDuration * currentPrecent; float playedTime = (float)m_iRawDuration * currentPrecent;
CCFrameData *from;
CCFrameData *to;
bool isListEnd;
//! If play to current frame's front or back, then find current frame again //! If play to current frame's front or back, then find current frame again
if (playedTime >= m_iTotalDuration || playedTime < m_iTotalDuration - m_BetweenDuration) if (playedTime < m_iTotalDuration || playedTime >= m_iTotalDuration + m_BetweenDuration)
{ {
/* /*
* Get frame length, if m_iToIndex >= _length, then set m_iToIndex to 0, start anew. * Get frame length, if m_iToIndex >= _length, then set m_iToIndex to 0, start anew.
* m_iToIndex is next index will play * m_iToIndex is next index will play
*/ */
int length = m_pMovementBoneData->frameList.count(); int length = m_pMovementBoneData->frameList.count();
CCFrameData *from = NULL;
CCFrameData *to = NULL;
bool isListEnd;
CCFrameData **frames = (CCFrameData**)m_pMovementBoneData->frameList.data->arr;
do do
{ {
m_BetweenDuration = m_pMovementBoneData->getFrameData(m_iToIndex)->duration; from = frames[m_iFromIndex];
m_iTotalDuration += m_BetweenDuration; to = frames[m_iToIndex];
m_iTotalDuration = from->frameID;
m_iFromIndex = m_iToIndex; m_iFromIndex = m_iToIndex;
if (++m_iToIndex >= length) if (++m_iToIndex >= length)
@ -393,27 +391,29 @@ float CCTween::updateFrameData(float currentPrecent, bool activeFrame)
m_iToIndex = 0; m_iToIndex = 0;
} }
} }
while (playedTime >= m_iTotalDuration); while (playedTime < from->frameID || playedTime>=to->frameID);
m_BetweenDuration = to->frameID - from->frameID;
isListEnd = m_eLoopType == ANIMATION_MAX && m_iToIndex == 0; isListEnd = m_eLoopType == ANIMATION_MAX && m_iToIndex == 0;
if(isListEnd) if(isListEnd)
{ {
to = from = m_pMovementBoneData->getFrameData(m_iFromIndex); to = from = frames[m_iFromIndex];
}
else
{
from = m_pMovementBoneData->getFrameData(m_iFromIndex);
to = m_pMovementBoneData->getFrameData(m_iToIndex);
} }
// else
// {
// from = frames[m_iFromIndex];
// to = frames[m_iToIndex];
// }
m_eFrameTweenEasing = from->tweenEasing; m_eFrameTweenEasing = from->tweenEasing;
setBetween(from, to); setBetween(from, to);
} }
currentPrecent = 1 - (m_iTotalDuration - playedTime) / (float)m_BetweenDuration; currentPrecent = (playedTime - m_iTotalDuration) / (float)m_BetweenDuration;
/* /*

View File

@ -124,7 +124,6 @@ protected:
CCFrameData *m_pTo; //! To frame data, used for calculate between value CCFrameData *m_pTo; //! To frame data, used for calculate between value
CCFrameData *m_pBetween; //! Between frame data, used for calculate current CCFrameData(m_pNode) value CCFrameData *m_pBetween; //! Between frame data, used for calculate current CCFrameData(m_pNode) value
CCFrameData *m_pCurrentKeyFrame; //! A weak reference to the current CCFrameData. The data is in the data pool
CCBone *m_pBone; //! A weak reference to the CCBone CCBone *m_pBone; //! A weak reference to the CCBone

View File

@ -269,7 +269,8 @@ CCBoneData *CCArmatureData::getBoneData(const char *boneName)
} }
CCFrameData::CCFrameData(void) CCFrameData::CCFrameData(void)
: duration(1) : frameID(0)
, duration(1)
, tweenEasing(Linear) , tweenEasing(Linear)
, displayIndex(0) , displayIndex(0)
@ -288,6 +289,7 @@ void CCFrameData::copy(CCFrameData *frameData)
{ {
CCBaseData::copy(frameData); CCBaseData::copy(frameData);
frameID = frameData->frameID;
duration = frameData->duration; duration = frameData->duration;
displayIndex = frameData->displayIndex; displayIndex = frameData->displayIndex;
tweenEasing = frameData->tweenEasing; tweenEasing = frameData->tweenEasing;
@ -313,7 +315,6 @@ bool CCMovementBoneData::init()
void CCMovementBoneData::addFrameData(CCFrameData *frameData) void CCMovementBoneData::addFrameData(CCFrameData *frameData)
{ {
frameList.addObject(frameData); frameList.addObject(frameData);
duration += frameData->duration;
} }
CCFrameData *CCMovementBoneData::getFrameData(int index) CCFrameData *CCMovementBoneData::getFrameData(int index)

View File

@ -285,6 +285,7 @@ public:
virtual void copy(CCFrameData *frameData); virtual void copy(CCFrameData *frameData);
public: public:
int frameID;
int duration; //! The frame will last m_iDuration frames int duration; //! The frame will last m_iDuration frames
CCTweenType tweenEasing; //! Every frame's tween easing effect CCTweenType tweenEasing; //! Every frame's tween easing effect

View File

@ -562,11 +562,20 @@ CCMovementBoneData *CCDataReaderHelper::decodeMovementBone(tinyxml2::XMLElement
CCFrameData *frameData = decodeFrame( frameXML, parentFrameXML, boneData); CCFrameData *frameData = decodeFrame( frameXML, parentFrameXML, boneData);
movBoneData->addFrameData(frameData); movBoneData->addFrameData(frameData);
frameData->frameID = totalDuration;
totalDuration += frameData->duration; totalDuration += frameData->duration;
movBoneData->duration = totalDuration;
frameXML = frameXML->NextSiblingElement(FRAME); frameXML = frameXML->NextSiblingElement(FRAME);
} }
//
CCFrameData *frameData = CCFrameData::create();
frameData->copy((CCFrameData*)movBoneData->frameList.lastObject());
frameData->frameID = movBoneData->duration;
movBoneData->addFrameData(frameData);
return movBoneData; return movBoneData;
} }
@ -1049,12 +1058,22 @@ CCMovementBoneData *CCDataReaderHelper::decodeMovementBone(cs::CSJsonDictionary
{ {
cs::CSJsonDictionary *dic = json.getSubItemFromArray(FRAME_DATA, i); cs::CSJsonDictionary *dic = json.getSubItemFromArray(FRAME_DATA, i);
CCFrameData *frameData = decodeFrame(*dic); CCFrameData *frameData = decodeFrame(*dic);
movementBoneData->addFrameData(frameData);
//movementBoneData->duration += frameData->duration; movementBoneData->addFrameData(frameData);
frameData->frameID = movementBoneData->duration;
movementBoneData->duration += frameData->duration;
delete dic; delete dic;
} }
//
CCFrameData *frameData = CCFrameData::create();
frameData->copy((CCFrameData*)movementBoneData->frameList.lastObject());
movementBoneData->addFrameData(frameData);
frameData->frameID = movementBoneData->duration;
return movementBoneData; return movementBoneData;
} }