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

Author Topic: Sprite Animation help  (Read 1033 times)

0 Members and 1 Guest are viewing this topic.

twiggystardust

  • Newbie
  • *
  • Posts: 8
    • View Profile
Sprite Animation help
« 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);         
                }
               
        }
}
 

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Sprite Animation help
« Reply #1 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. It provides some ready-to-use classes where you only have to specify attributes such as texture rect or animation length.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

twiggystardust

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Sprite Animation help
« Reply #2 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();               
        }
}