0
\$\begingroup\$

I'm making a basic Street Fighter style game in SFML, and I use Clock and Time() from SFML to time the animations. Here's the code I started with for my Idle() animation:

//frameTimer being Clock frameTimer;
void SpriteAnimator::Idle(Sprite & targetSprite, IntRect & targetRect)
{
    // changes x of target rect to proper point in sprite sheet to start animation
    if (targetRect.left < 0 || targetRect.left > 150)
        targetRect.left = 0;
    // every 0.2 seconds, change targetRect
    if (frameTimer.getElapsedTime().asSeconds() > 0.2) 
    {
        if (targetRect.left == 150)
            targetRect.left = 0;
        else
            targetRect.left += 50;
        targetSprite.setTextureRect(targetRect);
        frameTimer.restart(); // set Clock back to 0
    }
}

This works for one sprite, but I have the 2nd sprite use the same methods as the first sprite. They both are objects made from PLayer.H, and they both call an instance of SpriteAnimator to animate their respective sprite sheet.

The problem is, they're both running off the same clock, so when one sprite calls frameTimer.restart(); it messes both of them up, and only one can Idle() at a time.

How can I make it so they both can loop through their spritesheet at the same time, without the clocks overlapping?

(Btw my class structure is like this: main.cpp -->has player.cpp -->has spriteAnimator.cpp)

\$\endgroup\$
1
\$\begingroup\$

You could iterate over all your sprites in your main loop and set the textures using one clock, then reset the clock after the textures have been set.

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

Disclaimer: I have no experience with sfml.

I see two solutions to this problem:

  • use different Clocks
  • use deltatime instead of resetting the clock.

I think the deltatime aproach is the way to go. Just store the starting timestamp of your animation. With this approach: ElapsedTime = Now - StartTime. Everything with an animation has to have its own StartTime. Reset StartTime to Now at the end of the animation.
Somehow this is like an own clock, but if you pause the main clock, everything is paused.

\$\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.