I think the problem is, that you check in your “#handle sprite direction” part online for direction changes if the sprite is already flipped. remove the flip_h condition in your if statement and try it again
It probably has to do with the: “flip_h = direction”. What values does direction have? flip_h is a “bool” and direction is an “int” → the conversions are most likely not exactly what you want. consider using another if statement to fix that
elif sign(velocity.x) != sign(direction) && velocity.x != 0 && direction != 0:
flip_h = direction
the flip_h = direction I’m pretty sure gets evaluated to flip_h = direction != 0 which should always be true, you want it to flip if the direction is < 0 I believe
elif sign(velocity.x) != sign(direction) && velocity.x != 0 && direction != 0:
flip_h = direction < 0