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

Author Topic: Animation in thor does not play.  (Read 1999 times)

0 Members and 1 Guest are viewing this topic.

ebior123

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Animation in thor does not play.
« on: May 15, 2017, 07:20:13 pm »
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();
    }
}

 

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10821
    • View Profile
    • development blog
    • Email
Re: Animation in thor does not play.
« Reply #1 on: May 15, 2017, 07:26:16 pm »
I'm not 100% sure, but I think by calling playAnimation every frame you're resettingthe animation, thus you always just see the first frame.

You may also want to check out the latest version on GitHub, as the API for the animation bits has changed slightly.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

ebior123

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Re: Animation in thor does not play.
« Reply #2 on: May 15, 2017, 07:43:00 pm »
You wrote about thor, didn't you? I downloaded it maybe a week ago so i think it is up-to-date.

I thought i did it right because i had seen some posts on this forum and tutorial on thor website. How can i solve it then?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10821
    • View Profile
    • development blog
    • Email
Re: Animation in thor does not play.
« Reply #3 on: May 15, 2017, 07:53:33 pm »
By not calling playAnimation every frame. ;)

For example only call playAnimation when you press a button.
Check out the example code that ships with Thor.

Well if you download Thor 2.0 "latest" then yeah, you have the "latest" code and if you download the git version you get the development version which is newer and has some changes that aren't reflected in the tutorials yet.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

ebior123

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Re: Animation in thor does not play.
« Reply #4 on: May 15, 2017, 10:13:24 pm »
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.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10821
    • View Profile
    • development blog
    • Email
Re: Animation in thor does not play.
« Reply #5 on: May 16, 2017, 11:26:46 am »
The update and animate functions beed to be called every frame iteration and not just when you press a key.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

ebior123

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Re: Animation in thor does not play.
« Reply #6 on: May 16, 2017, 09:33:56 pm »
Thank you! Finally it works. You are the best