0
\$\begingroup\$

Hi guys I am trying to calculate the correct elapsedTime for each frame in my animation.

Example I have a sprite called stance that has 20 frames I want to give each frame a tick delay property that determines how long that frame will last until next frame.

the question is how do I use the gameTime.elapsedTime to calculate the current elapsedTicks say for example the first frame has 20 ticks then

currentElapsedTicks += gameTime.ElapsedTime.Ticks?

\$\endgroup\$
0
\$\begingroup\$

Add a timer to keep track of your elapsed time. Take a look at the following:

float Timer;
const float FRAMES_PER_SECOND = 20;
const float TIMER = 1 / FRAMES_PER_SECOND;

void Init()
{
    Timer = TIMER;
}

public void Update(GameTime gametime)
{
    float elapsed = (float)gametime.ElapsedGameTime.TotalSeconds;

    Timer -= elapsed;

    if (Timer <= 0)
    {
        ActiveFrame = NextFrame();
        Timer = TIMER;
    }
}
\$\endgroup\$
0
\$\begingroup\$

I managed this stuff in love-2d: Here how i've done

anim.started=false
anim.curFrame=0
anim.accum = 0

function anim:update(dt)
    if anim.started then
        anim.accum = anim.accum+dt          
        if anim.accum > anim.frameTime then
            anim.accum = anim.accum-anim.frameTime
            if anim.aType=="loop"then
                anim.curFrame=(anim.curFrame+1)%anim.nFrames                
            end
        end
    end
end

This is lua code , you cane consider it as pseudo code. "anim" is a kind of "class" the stores "pointers" to each animation frame. anim.curFrame is the "pointer" at the frame currently rendered.

The idea is to accumulate delta time in anim.accum until anim.accum become grater than anim.frameTime.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.