Guys, I spent whole day trying to find a solution to this. I don't know how to say to the game to show the Ryu's IDLE animation. The only thing that comes to mind is: If no key is pressed, show_ryu_idle(), so I need to check all the possible buttons on my keyboard every single frame, but I'm sure there is more elegant solution, but nothing comes to mind. I tried to to do it like this: if ( no other animation is currently been shown ) { set anim to idle }, but the problem is that the SDL_KEYDOWN doesnt work that way. For example if I press UP ARROW, and i hold the up arrow for 120 frames, it will count as the key was down only once. So if I set the animation when the key is pressed, it will only work for the 1st frame... If you understood something from this, tell me how you made your own games to handle this??
When to switch to hero_idle animation?
Afaik, SDL sends you "key pressed down" and "key released" events, ie it detects changes between on and off only.
If you want the current state of a button, you need to store the last event for that button, like
boolean button = false;
if (key_down) buton = true;
if (key_up) button = false;
and in your idle animation: if (!button) ....
So, I need to declare variables for all the buttons, and check every frame if they are false???( so i can make the IDLE anim)
If you want the idle animation when the user has nothing pressed, yes
You should probably keep the details of that logic eg in the user input somewhere, and compute "isIdle" there, which you can use in the remainder of the game.
One step further you can hide all key details that way. If you ever want to allow changing the used keys, that would be a simple local change.
You can also merge all those functions into one
enum KeyDirection {
KD_NONE, // or KD_IDLE
KD_LEFT,
KD_RIGHT,
KD_UP,
KD_DOWN,
};
KeyDirection getDirection(); // in the keyboard handler.
i use getAsyncKeyState to get similar keyboard info from the winAPI.
all input is processed immediately, not deferred for later processing.
once input is processed, i then figure out which animation to play, based on the input.
so yes, i check all the keys, then determine the animation.
For example: A player would have one action state set at a time, and you could select an animation depending on this. Idle animation could be played if the speed is == 0, and if they player is not currently in the process of another state such as falling or attacking.