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:
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!