SFML community forums

Help => General => Topic started by: MarbleXeno on August 03, 2020, 02:58:45 pm

Title: How to stop an animation or play it for only a certain number of frames
Post by: MarbleXeno on August 03, 2020, 02:58:45 pm
Hello, i'm looking for a solution to my problem, i can't figure out how to fix it.

So, i coded this simple animation class:
#pragma once
#include <SFML/Graphics.hpp>

class Animator
{
private:

        int framesPassed = 0;

        sf::Sprite* EntitySprite;
        sf::IntRect EntityRect;
        sf::Texture EntityTexture;

        sf::Clock clock;


public:
        Animator(sf::Sprite& sprite, sf::IntRect entityRect, std::string pathToSpriteSheet) {
                EntityTexture.loadFromFile(pathToSpriteSheet);
                EntitySprite = &sprite;
                EntityRect = entityRect;

                EntitySprite->setTexture(EntityTexture);
                EntitySprite->setTextureRect(EntityRect);
        }


        void applyAnimation(int intRectLeftAdd, float animationSpeed, int intRectResetAt, int intRectResetTo) {

                EntitySprite->setTextureRect(EntityRect);

                if (clock.getElapsedTime().asSeconds() > animationSpeed) {
                        EntityRect.left += intRectLeftAdd;
                        if (EntityRect.left >= intRectResetAt) {
                                EntityRect.left = intRectResetTo;
                        }
                        EntitySprite->setTextureRect(EntityRect);
                        framesPassed++;
                        clock.restart();

                }
        }


};
 

and its working correctly for repeating animations like walking, idle. But now i wan't to do a dead animation, but it cannot repeat, so i was trying to add variables like frames, but i dont know how to implement it, i know that i can use something like:

if(this.frames == framse) return;
 

but since its running every frame its resseting and not working.

So I don't know how to play the animation once (dead sprite has 4 frames) and after it reaches the end (last frame), I want to stop the animation, like freeze it on the last frame.
Title: Re: How to stop an animation or play it for only a certain number of frames
Post by: MarbleXeno on August 03, 2020, 08:57:00 pm
I was trying ad trying and i still can't do that :/
Title: Re: How to stop an animation or play it for only a certain number of frames
Post by: Stauricus on August 04, 2020, 12:54:05 pm
since I have no idea how you structured the rest of your code, i'm going to just suggest you to make a bool inside your class named "m_animation_playing" or something like that. when your character dies, you wait for the deah animation to finish and then turn the bool off. while it is off, you dont change the EntityRect position.
Title: Re: How to stop an animation or play it for only a certain number of frames
Post by: MarbleXeno on August 04, 2020, 01:02:42 pm
Okay, so after a sleep (sleeping is really good for fixing the problems) I did something like that

        void applyAnimation(int intRectLeftAdd, float animationSpeed, int intRectResetAt, int intRectResetTo, bool repeat) {

                EntitySprite->setTextureRect(EntityRect);

                if (clock.getElapsedTime().asSeconds() > animationSpeed) {
                        EntityRect.left += intRectLeftAdd;
                        if (EntityRect.left >= intRectResetAt) {
                                if (repeat == false) {
                                        EntityRect.left = intRectResetAt;
                                }
                                else {
                                        EntityRect.left = intRectResetTo;

                                }
                        }
                        EntitySprite->setTextureRect(EntityRect);
                        clock.restart();

                }
        }


};
 

I know that this looks awful but i dont have any other idea, it works, for now.