SFML community forums

Help => General => Topic started by: twiggystardust on December 15, 2013, 09:51:12 pm

Title: Sprite Animation help
Post by: twiggystardust on December 15, 2013, 09:51:12 pm
I have been trying to work this out on my own but I cannot figure it out. This gets all the frames in place, but it displays them all at the same time. I wanted it to cycle through them. I tried putting everything in the update function, but then it doesn't display any of the frames in the correct place and it's all at the same time. So I moved it into the Draw function and as I said it displays them all at the same time. Any suggestions?

#include "headers.h"
#include "SwordSprite.h"

SwordSprite::SwordSprite()
{
        Load("images/aGameSwordSpriteSheet.png");
        assert(IsLoaded());
}

SwordSprite::~SwordSprite()
{
}

void SwordSprite::Update(float elapsedTime)
{

}

void SwordSprite::Draw(sf::RenderWindow &swin)
{
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
        {
                int curFrame = 0;
            int maxFrame = 2;
                SPRITE_HEIGHT = 35;
                SPRITE_WIDTH = 44;

                for(curFrame = 0; curFrame <= maxFrame; curFrame++)
                {
                        GetSprite().setTextureRect(sf::IntRect(SPRITE_WIDTH * curFrame, 0
                     , SPRITE_WIDTH, SPRITE_HEIGHT));

                        GetSprite().setOrigin((GetSprite().getTextureRect().width / 2) - 9.0f
                     , (GetSprite().getTextureRect().height / 2) + 2.0f);
                       
                                       
                        VisibleGameObject::Draw(swin);         
                }
               
        }
}
 
Title: Re: Sprite Animation help
Post by: Nexus on December 15, 2013, 10:01:27 pm
The for loop is wrong: you don't want to cycle through all animations at once, you want to wait some time before the next frame is shown. By the way, it's cleaner not to mix input handling (sf::Keyboard::isKeyPressed()) with drawing, and to encapsulate members (GetSprite() returns a reference, which is questionable).

If you don't want to implement animations on your own, you can have a look at Thor's Animation system (http://www.bromeon.ch/libraries/thor/v2.0/tutorial-animation.html). It provides some ready-to-use classes where you only have to specify attributes such as texture rect or animation length.
Title: Re: Sprite Animation help
Post by: twiggystardust on December 16, 2013, 02:39:39 am
Well I was planning on implementing the Animation myself, just something I like to do. So I moved everything into the update function. I also tried adding a clock/timer, I'm not sure this is the best or even a good way of implementing the timer but it was the best I could come up with in an hour and a half. but now it just shows all three frames next to the sprite and each frame next to each other. I'm at a loss here.

void SwordSprite::Update(float elapsedTime)
{
        sf::Clock clock;


        sf::Time time = sf::seconds(1.0f);

       

        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
        {
            int maxFrame = 2;
                SPRITE_HEIGHT = 35;
                SPRITE_WIDTH = 44;

                if(clock.getElapsedTime() >= time)
                {
                        for(int curFrame = 0; curFrame <= maxFrame; curFrame++)
                        {
                                GetSprite().setTextureRect(sf::IntRect(SPRITE_WIDTH * curFrame, 0
                                , SPRITE_WIDTH, SPRITE_HEIGHT));
                                GetSprite().setOrigin((GetSprite().getLocalBounds().width / 2) - 9.0f
                                , (GetSprite().getLocalBounds().height / 2) + 2.0f);
                        }                      
                }

                VisibleGameObject::Update(elapsedTime);
                clock.restart();               
        }
}