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?