Hi, I have got a problem with animation. I added frames to thor::FrameAnimation and next added it to thor::animator. Everything what it do is showing only one frame. I researched my problem and found nothing. I am using current version of SFML and thor on Xcode on mac. Could you help me?
I cut out only a few lines of code. In project i have it in separate classes.
thor::Animator<sf::Sprite, std::string> animator;
thor::FrameAnimation moveLeft;
sf::Sprite sPlayer;
sPlayer.setTexture(Textures);
sPlayer.setTextureRect(sf::IntRect (362, 248, 37, 59)); //set starting texture
moveLeft.addFrame(1.f, sf::IntRect (320, 304, 42, 58));
moveLeft.addFrame(2.f, sf::IntRect (320, 186, 42, 59));
moveLeft.addFrame(3.f, sf::IntRect (320, 304, 42, 58));
moveLeft.addFrame(4.f, sf::IntRect (320, 186, 42, 59));
animator.addAnimation("moveLeft", moveLeft, sf::seconds(4));
while (window.isOpen()) // Start the game loop
{
ProcessEvents();
Render();
animator.playAnimation("moveLeft");
animator.update(Clock.restart());
animator.animate(sPlayer);
window.draw(sPlayer);
window.display();
window.clear();
}
}
Oh, i see now. Got it.
I moved that part of code to present it in better way. This is how it looks in my project
void engine::ProcessEvents()
{
while (window.pollEvent(event))
{
switch (event.type)
{
case sf::Event::KeyPressed:
handlePlayerInput(event.key.code, true);
break;
case sf::Event::KeyReleased:
handlePlayerInput(event.key.code, false);
break;
case sf::Event::Closed:
window.close();
break;
}
}
}
void engine::handlePlayerInput(sf::Keyboard::Key key, bool isPressed)
{
if(key == sf::Keyboard::W && isPressed == true)
{
Player.movePlayer(0);
}
else if(key == sf::Keyboard::S && isPressed == true)
{
Player.movePlayer(1);
}
else if(key == sf::Keyboard::A && isPressed == true)
{
Player.movePlayer(2);
}
else if(key == sf::Keyboard::D && isPressed == true)
{
Player.movePlayer(3);
}
}
void cPlayer::movePlayer(unsigned short direction)
{
if(direction == 0) //up
{
Player.move(0, -Speed);
animator.playAnimation("moveUp", true);
}
if(direction == 1) //down
{
Player.move(0, Speed);
animator.playAnimation("moveDown", true);
}
if(direction == 2) //left
{
Player.move(-Speed, 0);
animator.playAnimation("moveLeft", true);
}
if(direction == 3) //right
{
Player.move(Speed, 0);
animator.playAnimation("moveRight", true);
}
animator.update(Clock.restart());
animator.animate(Player);
}
Sometimes when i press a few times same key, for example "W" then texture change to next but stay static.