How do I insure that my game isn’t reading two direction buttons effectively at the “same time”? Is there an easy way to have it read Up without Left and Right reading also?
Thanks
How do I insure that my game isn’t reading two direction buttons effectively at the “same time”? Is there an easy way to have it read Up without Left and Right reading also?
Thanks
Realistically up and left or right should be pressable at the same time.
If you want to only react to one regardless of how many are pressed you could check each in an if-elseif statement.
if (arduboy.pressed(UP_BUTTON))
{
//Do stuff
}
else if (arduboy.pressed(LEFT_BUTTON))
{
// Do other stuff
}
//Do additional else if statements for each desired button.
One thing to be wary of with using else
s on directional buttons is that you’ll inevitably end up giving priority to one of the buttons.
E.g. in the example given, if the user presses left and then up, the code will react as if only up was pressed, and if the user presses up and then left, the code will still react as if only up was pressed because up has priority.
That may or may not be an issue, but if it does become an issue then there are a few ways to solve it depending on what behaviour would be more useful.
E.g. whether the key that was pressed first should take priority, whether the most recently pressed key should take priority, whether none of the keys should have priority and motion should stop…
Useless trivia: Strictly speaking C++ doesn’t actually have an ‘elseif’ statement in the same way some other languages do (e.g. Lua). In C++'s case it’s just an else
with an if
for a body. Not that the programmer is ever going to notice the difference. (A parser or compiler certainly will though.)
I did figure out a way to have one key work at a time. But this made the controls very stifled. Anyway I figured another work arround so I shold be okay.