Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: How to stop an animation or play it for only a certain number of frames  (Read 156 times)

0 Members and 1 Guest are viewing this topic.

MarbleXeno

  • Newbie
  • *
  • Posts: 26
    • View Profile
    • Email
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.
« Last Edit: August 03, 2020, 03:03:11 pm by MarbleXeno »

MarbleXeno

  • Newbie
  • *
  • Posts: 26
    • View Profile
    • Email
Re: How to stop an animation or play it for only a certain number of frames
« Reply #1 on: August 03, 2020, 08:57:00 pm »
I was trying ad trying and i still can't do that :/

Stauricus

  • Full Member
  • ***
  • Posts: 191
    • View Profile
    • Email
Re: How to stop an animation or play it for only a certain number of frames
« Reply #2 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.

MarbleXeno

  • Newbie
  • *
  • Posts: 26
    • View Profile
    • Email
Re: How to stop an animation or play it for only a certain number of frames
« Reply #3 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.