Skip to main content
fixed typos & code markdown, adjusted game description wording to reflect community values regarding IP
Source Link
Pikalek
  • 13.4k
  • 5
  • 49
  • 54

ImI'm making a basic Street Fighter ripoffstyle game in SFML, and iI use ClockClock and Time()Time() from SFML to time theirthe animations. HeresHere's the code I started with for my Idle()Idle() animation.:

//frameTimer being Clock frameTimer;
void SpriteAnimator::Idle(Sprite & targetSprite, IntRect & targetRect)
{
    if (targetRect.left < 0 || targetRect.left > 150) // 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) // every 0.2 seconds, change targetRect
    {
        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 SpriteAnimatorSpriteAnimator to animate their respective sprite sheet.

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

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

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

Im making a basic Street Fighter ripoff in SFML, and i use Clock and Time() from SFML to time their animations. Heres the code I started with for my Idle() animation.

//frameTimer being Clock frameTimer;
void SpriteAnimator::Idle(Sprite & targetSprite, IntRect & targetRect)
{
    if (targetRect.left < 0 || targetRect.left > 150) //changes x of target rect to proper point in sprite sheet to start animation
        targetRect.left = 0;
    if (frameTimer.getElapsedTime().asSeconds() > 0.2) // every 0.2 seconds, change targetRect
    {
        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)

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)

Source Link

SFML - How to animate two Sprites with the same Clock?

Im making a basic Street Fighter ripoff in SFML, and i use Clock and Time() from SFML to time their animations. Heres the code I started with for my Idle() animation.

//frameTimer being Clock frameTimer;
void SpriteAnimator::Idle(Sprite & targetSprite, IntRect & targetRect)
{
    if (targetRect.left < 0 || targetRect.left > 150) //changes x of target rect to proper point in sprite sheet to start animation
        targetRect.left = 0;
    if (frameTimer.getElapsedTime().asSeconds() > 0.2) // every 0.2 seconds, change targetRect
    {
        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)