Howdy, I'm trying to make a sprite animate with the Thor library when I press a key and stop animating when I release it.
This is pretty easy with event polling. I just did this:
while(mainWindow.PollEvent(Event))
{
...
if((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Keyboard::Right))
animator.PlayAnimation("walk", true);
if((Event.Type == sf::Event::KeyReleased) && (Event.Key.Code == sf::Keyboard::Right))
animator.StopAnimation();
if((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Keyboard::Left))
animator.PlayAnimation("walk", true);
if((Event.Type == sf::Event::KeyReleased) && (Event.Key.Code == sf::Keyboard::Left))
animator.StopAnimation();
}
//and a thing down here that stops the animation if both keys are pressed at the same time
Unfortunately, this gets totally messed up if you happen to press both left and right at the same time, and then release one of those while still holding the other. Since no new key pressed event was fired there, the sprite will be unanimated even though a key is down (therefore my sprite is sliding along without being animated).
The alternative to polling is the real-time input or w/e, but since that calls every frame, it'll screw up animator.PlayAnimation by calling it over and over while the key is pressed and restarting the animation before it even has time to reach its second frame.
I know I can do some fancy boolean state stuff to fix this (e.g. have some bool called isWalking or something, and do some things with it to circumvent a continuously restarting PlayAnimation call), but I can't logic it out myself.
Can you help a brother out?