I'm currently handling sprite animation with a switch that changes the animation based on the directional arrow pressed. I'm using an SDL_Event switch to do so. The problem is lets say I hold down left, then while keeping left pressed i press right, then I let go of right and left is still pressed.
This is what happens.
Press left - left running animation plays
While still holding left I pres right - right animation plays
I let go of right and have left still pressed - default animation plays(idle)
Is it better to use a while loop or an if statement for this? What would you guys recommend?
A switch is preferable I think because it expresses what you're doing better: you're taking different actions based on the value of a single expression.
To solve your problem, you need to look at the current state of the keys. So the logic should be something like:
1 2 3 4
wait for key up or key down.
switch(getKeyState()) {
...
}
Inside getKeyState() you will check to see which (if any) arrow keys are down. Decide what should happen if multiple keys are pressed before you start writing the code.