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

Author Topic: Trouble changing back to idle animation after walking  (Read 1235 times)

0 Members and 1 Guest are viewing this topic.

delagore

  • Newbie
  • *
  • Posts: 3
    • View Profile
Trouble changing back to idle animation after walking
« on: March 04, 2021, 01:46:38 am »
Hi all! Newbie developer here (I'm a first year student, new to coding.) I am having some issues with a game I am trying to write. It's a pretty standard 2D, side scrolling platformer type.

I am having some trouble going back to an idle animation after walking. It returns to the idle animation after the other animations I have so far coded (jumping, attacking), but it just keeps looping in the walk animation when I release the arrow key, even though the sprite is no longer moving. The code is:

Quote
void Player::movement(float dir, float speed, float deltaTime)
{

   // Sets the direction and speed, calculates the velocity and sets a new position
   direction = sf::Vector2f(dir, 0);
   setVelocity(direction * speed);
   setPosition(getPosition() + (getVelocity() * deltaTime));
   
}
void Player::handleInput(float deltaTime)
{
          // Calls the movement function when the arrow key is pressed
         if (input->isKeyDown(sf::Keyboard::Right))
    {
               movement(10.f, 5.f, deltaTime);
              walkAnimation();
    }
}

void Player::update(float deltaTime)
{
        if (isAnimationOver() && !input->isKeyDown(sf::Keyboard::Right))
      {
            idleAnimation();
      }
}

I'm happy to include more code, I just didn't want to put in too much unnecessary stuff. As I said, the isAnimationOver() function called in the update works to return to idle after the other animations, and the sprite stops moving when I release the arrow key, but for some reason the walk animation won't stop looping. If I include a line of code to call idleAnimation() on a key press it also returns to the idle animation with no problem, so I assume it's something to do with not having a clear enough event to call idle, but I'm completely stumped as to what to do.

Thanks in advance, any guidance, advice or help is greatly appreciated!

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10846
    • View Profile
    • development blog
    • Email
Re: Trouble changing back to idle animation after walking
« Reply #1 on: March 09, 2021, 12:32:29 pm »
Are you correctly resetting your key state when the key is released?

I highly recommend to familiarize yourself with the debugger and step through the code, checking all the parts.
It can be a bit annoying since you depend on key input, but with break points you should be able to stop whenever something is reached and if it's not reached, move it further up until you find the condition that is making things not work properly.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

delagore

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Trouble changing back to idle animation after walking
« Reply #2 on: March 10, 2021, 01:37:44 pm »
Are you correctly resetting your key state when the key is released?

I highly recommend to familiarize yourself with the debugger and step through the code, checking all the parts.
It can be a bit annoying since you depend on key input, but with break points you should be able to stop whenever something is reached and if it's not reached, move it further up until you find the condition that is making things not work properly.

Thanks for the reply and the advice! As it happened I was adding my animation frames in as part of the function to call the animation instead of adding them in the constructor, so my isAnimationOver() function wasn't calling true. I am trying to familiarize myself with the debugger though (we just had  a lesson on it yesterday!), it looks like a useful tool!

 

anything