Foaly: I have decided to go with yours as it was easiest for me to understand. I want to point out that there was some error in my code with window handling because your code also got rid of an exit error I have been having and posted in another tread!
Assuming that it is somewhat efficient, that is the best I could do with my current knowledge. It works completely! Any further improvements would be appreciated, but with some explanation as I really fried my brain figuring that out.
I have one issue though, where do I enable the loop for the animation?
#include <SFML/Graphics.hpp>
#include "AnimatedSprite.hpp"
int main()
{
sf::Vector2i screenDimensions(800,600);
sf::RenderWindow window(sf::VideoMode(screenDimensions.x, screenDimensions.y), "Animations!");
// load texture
sf::Texture texture;
texture.loadFromFile("Player.png");
// push frames
Animation walkingAnimationDown;
walkingAnimationDown.setSpriteSheet(texture);
walkingAnimationDown.addFrame(sf::IntRect(0, 0, 32, 32));
walkingAnimationDown.addFrame(sf::IntRect(32, 0, 32, 32));
walkingAnimationDown.addFrame(sf::IntRect(64, 0, 32, 32));
Animation walkingAnimationLeft;
walkingAnimationLeft.setSpriteSheet(texture);
walkingAnimationLeft.addFrame(sf::IntRect(0, 32, 32, 32));
walkingAnimationLeft.addFrame(sf::IntRect(32, 32, 32, 32));
walkingAnimationLeft.addFrame(sf::IntRect(64, 32, 32, 32));
Animation walkingAnimationRight;
walkingAnimationRight.setSpriteSheet(texture);
walkingAnimationRight.addFrame(sf::IntRect(0, 64, 32, 32));
walkingAnimationRight.addFrame(sf::IntRect(32, 64, 32, 32));
walkingAnimationRight.addFrame(sf::IntRect(64, 64, 32, 32));
Animation walkingAnimationUp;
walkingAnimationUp.setSpriteSheet(texture);
walkingAnimationUp.addFrame(sf::IntRect(0, 96, 32, 32));
walkingAnimationUp.addFrame(sf::IntRect(32, 96, 32, 32));
walkingAnimationUp.addFrame(sf::IntRect(64, 96, 32, 32));
// set up AnimatesSprite
AnimatedSprite animatedSprite(sf::seconds(0.2));
animatedSprite.setAnimation(walkingAnimationDown);
animatedSprite.pause();
animatedSprite.setPosition(0,0);
sf::Clock frameClock;
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape)
window.close();
if(event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Up)
{
animatedSprite.setAnimation(walkingAnimationUp);
animatedSprite.play();
}
else if(event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Down)
{
animatedSprite.setAnimation(walkingAnimationDown);
animatedSprite.play();
}
else if(event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Left)
{
animatedSprite.setAnimation(walkingAnimationLeft);
animatedSprite.play();
}
else if(event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Right)
{
animatedSprite.setAnimation(walkingAnimationRight);
animatedSprite.play();
}
else
{
animatedSprite.pause();
}
/*if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::P)
{
if(animatedSprite.isPlaying())
animatedSprite.pause();
else
animatedSprite.play();
}*/
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
{
animatedSprite.move(0, -.25);
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
{
animatedSprite.move(0, .25);
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
{
animatedSprite.move(.25, 0);
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
{
animatedSprite.move(-.25, 0);
}
// update AnimatedSprite
animatedSprite.update(frameClock.restart());
window.clear();
window.draw(animatedSprite);
window.display();
}
return 0;
}