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);
}
}
}
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();
}
}