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

Author Topic: Animate sprite back to idle state  (Read 2013 times)

0 Members and 1 Guest are viewing this topic.

Trinity

  • Newbie
  • *
  • Posts: 10
    • View Profile
Animate sprite back to idle state
« on: August 19, 2017, 06:59:58 pm »
Hey!

I have recently picked up SFML and decided to try out the extension, "Thor", which comes with some handy
functionality. Although I'm experiencing some problems when I'm trying to animate my sprite i.e return it to idle state when I have released the key which should trigger the movement. The code up to this point looks like this:

void Player::UpdateInput(sf::Time dt)
{
        float velocity = m_movementSpeed * dt.asSeconds();

        if (Input::GetActionMap().isActive("walkRight"))
        {
                if (!m_animator.isPlayingAnimation())
                        m_animator.playAnimation("walkRight");

                if (m_animator.getPlayingAnimation() != "walkLeft")
                        m_playerSprite.move(velocity, 0.0f);
                else
                        m_animator.stopAnimation();
        }

        if (Input::GetActionMap().isActive("walkLeft"))
        {
                if (!m_animator.isPlayingAnimation())
                        m_animator.playAnimation("walkLeft");

                if (m_animator.getPlayingAnimation() != "walkRight")
                        m_playerSprite.move(-velocity, 0.0f);
                else
                        m_animator.stopAnimation();
        }
}

Now the behaviour is perfectly fine except from that the sprite doesn't stop animating directly when I release the chosen movement key, something which could be solved by setting up a release event like this:

if (Input::GetActionMap().isActive("stopRight"))
{
        if (m_animator.isPlayingAnimation() && m_animator.getPlayingAnimation() == "walkRight")
        {
                m_animator.stopAnimation();
                m_animator.playAnimation("idleRight");
        }
}

What will happen now is that when I move right for example, the sprite will stop animating and then return to idle. However the movement of the sprite will not be in sync with the animation since the idleRight animation is still set to one second. Reducing it to around 0.1 seconds solves the problems but allows the player to simply glide along when you tap the movement key  :'(. I have a feeling there is a more elegant solution to this, perhaps I should look into blend trees for animating?


Trinity

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Animate sprite back to idle state
« Reply #1 on: August 20, 2017, 02:40:45 pm »
I believe I solved it by doing what I did above but instead of adding the idle state, I added the last frame on the animation cycle when the key is released. Because if I don't and have the idle state, the animation will start from the idle state and therefore play the idle frame one more time  :-\.