Game Development Reference
In-Depth Information
void Start(unsigned int frame=0)
{
if ( frame >= m_animation.FrameCount() )
return;
m_currentFrame = frame;
auto& currentFrame = m_animation.Frame(m_currentFrame);
m_time = currentFrame.Duration();
}
void Update(float deltaTime);
const animation::frame& CurrentFrame() const { return m_animation.Frame(m_currentFrame); }
private:
float m_time;
unsigned int m_currentFrame;
const animation& m_animation;
};
Theanimationtrackisconstructedwithagivenanimation'sdata,wemustcallStarttospe-
cify the frame from which we want to begin animating, usually this will be the first frame.
The Update function is responsible for advancing the animation track each game frame.
void animation_track::Update(float deltaTime)
{
if ( m_animation.FrameCount() == 0 || (!m_animation.Loop() && m_currentFrame > m_animation.FrameCount() ) )
{
return;
}
m_time -= deltaTime;
if ( m_time <= 0.f )
{
++m_currentFrame;
if ( m_currentFrame >= m_animation.FrameCount() )
{
if ( m_animation.Loop() )
{
m_currentFrame = 0;
}
else
{
m_onAnimationFinished.Invoke(this, *this);
}
}
auto& currentFrame = m_animation.Frame(m_currentFrame);
m_time = currentFrame.Duration();
}
}
The m_time variable is set to the current frame's duration, each game frame we decrement
it, once it reaches zero or less we advance to the next frame. By default the sprite will loop
Search WWH ::




Custom Search