以下代码替换整个update即可
void AnimationTimelineState::update(float time)
{
const auto prevTime = this->_currentTime;
const auto prevPlayTimes = this->_currentPlayTimes;
if (!this->_isCompleted && this->_setCurrentTime(time))
{
const auto eventDispatcher = this->_armature->_display;
if (!_isStarted)
{
_isStarted = true;
if (eventDispatcher->hasEvent(EventObject::START))
{
const auto eventObject = BaseObject::borrowObject<EventObject>();
eventObject->animationState = this->_animationState;
this->_armature->_bufferEvent(eventObject, EventObject::START);
}
}
if (this->_keyFrameCount)
{
const auto currentFrameIndex = this->_keyFrameCount > 1 ? (unsigned)(this->_currentTime * this->_frameRate) : 0;
auto curPlayTimes = prevPlayTimes;
auto executeCount = 0;
if (curPlayTimes == this->_currentPlayTimes)
{
executeCount = currentFrameIndex - this->_currentFrameIndex;
}
else {
executeCount += this->_keyFrameCount - this->_currentFrameIndex - 1;
/*** 当前播放次数是从0开始的,所以当前播放次数达到动作播放次数时就不会在改变当前帧下标的数据了。
*** 也就不在需要在补算当前播放次数的帧数了。
***///
if (this->_animationState->playTimes == 0 || this->_currentPlayTimes < this->_animationState->playTimes) {
executeCount += currentFrameIndex + 1;
}
if (this->_currentPlayTimes - curPlayTimes > 1) {
executeCount += (this->_currentPlayTimes - curPlayTimes - 1) * this->_keyFrameCount;
}
}
// 容错处理
if (executeCount < 0) {
executeCount = currentFrameIndex;
this->_currentFrameIndex = -1;
}
for (size_t i = 0; i < executeCount; i++)
{
++this->_currentFrameIndex;
const auto currentFrame = this->_timeline->frames[this->_currentFrameIndex];
if (this->_currentFrame != currentFrame)
{
if (this->_keyFrameCount > 1)
{
auto crossedFrame = this->_currentFrame;
this->_currentFrame = currentFrame;
if (!crossedFrame)
{
const auto prevFrameIndex = (unsigned)(prevTime * this->_frameRate);
crossedFrame = this->_timeline->frames[prevFrameIndex];
if (this->_isReverse)
{
}
else
{
if (
prevTime < crossedFrame->position ||
prevPlayTimes != this->_currentPlayTimes
)
{
crossedFrame = crossedFrame->prev;
}
}
}
if (this->_isReverse)
{
while (crossedFrame != currentFrame)
{
this->_onCrossFrame(crossedFrame);
crossedFrame = crossedFrame->prev;
}
}
else
{
while (crossedFrame != currentFrame)
{
crossedFrame = crossedFrame->next;
this->_onCrossFrame(crossedFrame);
}
}
}
else
{
this->_currentFrame = currentFrame;
this->_onCrossFrame(this->_currentFrame);
}
}
if (this->_currentFrameIndex == this->_keyFrameCount -1)
{
if (eventDispatcher->hasEvent(EventObject::LOOP_COMPLETE))
{
const auto eventObject = BaseObject::borrowObject<EventObject>();
eventObject->animationState = this->_animationState;
this->_armature->_bufferEvent(eventObject, EventObject::LOOP_COMPLETE);
}
this->_currentFrameIndex = -1;
this->_currentFrame = nullptr;
}
}
}
if (prevPlayTimes != this->_currentPlayTimes)
{
if (this->_isCompleted && eventDispatcher->hasEvent(EventObject::COMPLETE))
{
const auto eventObject = BaseObject::borrowObject<EventObject>();
eventObject->animationState = this->_animationState;
this->_armature->_bufferEvent(eventObject, EventObject::COMPLETE);
}
this->_currentFrame = nullptr;
}
}
}