arduboy.justPressed() question

Is it true that justPressed() will return true only on one frame per button press?

Yes, but only on the Arduboy2 library and only if you remember to call pollButtons() at the start of each loop.

1 Like

is it possible for justPressed to return two buttons if you pressed A and B at the same frame?

Possibly, but it’s extremely unlikely for both buttons to be registered as being pressed on the exact same frame since most games run at 30 or 60 fps.

Disregard that question. Thinking about it more made me realize that it didn’t make much sense.

Let’s say you have a menu that accepts A or B input which changes the game state.

And to increase the odds, lets say the game runs at a silky smooth cinematic 15fps

If you monitor “justPressed” on both A and B and the user presses both at the same time wouldn’t you have to also implement a way of dropping the both inputs if justPressed A and B are both true?

Or for a menu system would you use some sort of switch statement?

depends on the type of menu you are developing, and the context it is used in. I tend to do things on the released state, because it feels more natural and is safer.

consider, if you ever have a case where pressing A in a menu takes you to a screen where holdingA down has some meaning. Eg, maybe holding A charges up the shot on some gun. If you do menu actions on justPressed. Then if you just pressed A to take you to the game view, you will immediately be pressing A, and charging up your shot. If you do your action on release of a button, you don’t run into these cases.

But, it’s very context related.


It depends.

If you’re using code like this:

if(arduboy.justPressed(A_BUTTON)) { doSomethingA(); }
if(arduboy.justPressed(B_BUTTON)) { doSomethingB(); }

Then both doSomethingA and soSomethingB will happen.

If you’re using code like this:

if(arduboy.justPressed(A_BUTTON)) { doSomethingA(); }
else if(arduboy.justPressed(B_BUTTON)) { doSomethingB(); }

Then only doSomethingA() happens because it takes precedence.

Like @Bergasms says, sometimes it makes more sense to check for when a button is released rather than when it’s pressed.

In reality the likelihood of justPressed(A_BUTTON) && justPressed(B_BUTTON) being true is pretty slim in most cases. (And it’s not necessarily tied to the fps of the game, it’s tied to how often you call pollButtons, which depending on your setup may or may not be tied to the game’s framerate.)

1 Like

I understand what you’re saying but I think you don’t understand the difference between justPressed and Pressed

IIRC justPressed only returns true on the frame it is pressed on. You are thinking of pressed()

No, Bergasms isn’t getting confused. Re-read what they said, it’s about a flaw with using justPressed for pause menus.

In particular how using justPressed to check if the player wants to un-pause can lead to causing some in-game action (like a weapon charging/being fired) reliant on a button being pressed to be triggered the moment the player unpauses and how using ‘justReleased’ instead of ‘justPressed’ resolves this issue.

No, definitely not the case :stuck_out_tongue: Probably my explanation was bad.

If you have a menu screen that responds to justPressed, then when you ‘justPressed’ a button, eg, you pressed it down, the menu action will be taken on that action. Now, the state of the button will be Pressed, and you will be in the new state.

Consider two states, X and Y.
You can transition from X -> Y by pressing A, the event happening on justPressed.
In state Y, holding A fires a missile, or releasing A launches a spaceship, or whatever.

So you are in X. Now, you press A.
The justPressed state of A causes you to move from X to Y.
Now you are in state Y, and immediately you are firing the A.pressed case, and have to also fire the A.justReleased case before you get back to the no buttons case, and you cannot escape that situation.

If you fire your menu operations on justReleased, then you move to the Y state with no buttons pressed. As I said, it is context based, but I believe you misunderstood my explanation.

Okay that makes sense. I have yet to receive my arduboy so I still need time to play around with running code.

May I recommend giving an emulator a simulator a try?
(I pray one of these still functions.)

1 Like

Clouduboy isn’t an emulator. The other two only emulate the Arduboy library, not Arduboy2, so the justPressed() and pollButtons() functions won’t be available (you could add their equavilents locally to the sketch, though).

Eh, don’t shoot the reposter