I am always happy to see my class being useful and to see it in action.
Please post a complet and minimal code of what is not working. Without that I can't help you…
Have you checked the return value of loadFromFile?
I made a simple animation test project using your class and the same sprite sheet, and it works, but trying to use it in my game does not work. Running the game results of one of 3 results:
1)
Unhandled exception at 0x003013EF in Game.exe: 0xC0000005: Access violation reading location 0x0000004F.
It points to this line in AnimatedSprite.cpp, in the setFrame function:
sf::IntRect rect = m_animation->getFrame(m_currentFrame);
2)
Unhandled exception at 0x590765CE (sfml-graphics-2.dll) in Game.exe: 0xC0000094: Integer division by zero.
It points to this line in AnimatedSprite.cpp, in the draw function:
target.draw(m_vertices, 4, sf::Quads, states);
3) The screen flashes black and white, occasionally settling at black.
These results happen randomly, with 3) being the most common.
This is the file that implements the animation:
#include "PlayState.h"
#include "resources.h"
;PlayState PlayState::instance;
void PlayState::activate()
{
sf::Texture texture;
if (!texture.loadFromFile(FILE_PLAYER))
throw std::runtime_error("Could not open " + FILE_PLAYER);
Animation animation;
animation.setSpriteSheet(texture);
for (int i = 0; i < 8; ++i)
{
animation.addFrame(sf::IntRect(i * 16, 0, 16, 48));
}
player = AnimatedSprite(sf::milliseconds(30));
player.setAnimation(animation);
}
void PlayState::input()
{
}
void PlayState::update(sf::Time delta)
{
player.update(delta);
}
void PlayState::render(sf::RenderWindow &window)
{
window.draw(player);
}
void PlayState::deactivate()
{
}
I can't understand how it would work in the test, but not here :O
Just in case, here's the code I used to test it:
#include <SFML/Graphics.hpp>
#include "AnimatedSprite.h"
int main()
{
sf::RenderWindow window(sf::VideoMode(800, 600), "Animation Test");
sf::Texture texture;
if (!texture.loadFromFile("spritesheet.png"))
return 1;
Animation animation;
animation.setSpriteSheet(texture);
for (int i = 0; i < 8; ++i)
{
animation.addFrame(sf::IntRect(i * 16, 0, 16, 48));
}
AnimatedSprite sprite(sf::milliseconds(100));
sprite.setAnimation(animation);
sf::Clock clock;
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
sprite.update(clock.restart());
window.clear();
window.draw(sprite);
window.display();
}
return 0;
}