LoginLogin

Button Dilemma

Root / Programming Questions / [.]

rotatingdiscCreated:
So from what I can tell, the
BUTTON(0)
return thing returns a floating byte made of binary bits that correspond to each buttons status. The thing is, that means if you press 2 buttons together, neither will register for their corresponding bytes. The question is, is there a way to set a variable to a specific bit of the button byte to dodge this issue? I'm relatively new to this program btw.

So from what I can tell, the
BUTTON(0)
return thing returns a floating byte made of binary bits that correspond to each buttons status. The thing is, that means if you press 2 buttons together, neither will register for their corresponding bytes. The question is, is there a way to set a variable to a specific bit of the button byte to dodge this issue? I'm relatively new to this program btw.
I'm not completely sure what you are asking, but I think this should work if I have you right.
SPSET 0,500
WHILE 1
BT=BUTTON() 'THIS IS A VARIABLE FOR HOLDING DOWN A BUTTON
RIGHT=#RIGHT
LEFT=#LEFT
SPOFS 0,X,Y

IF BT AND RIGHT THEN INC X
IF BT AND LEFT THEN DEC X
VSYNC 1
WEND
Try this.

That does certainly help with things like left and right, I'm more so looking for a way to do something like store the buttons b01 b11 etc. values as an array then set variables to those specific array values.

Remember, buttons all have whole numbers on their own and can act just like any other number. For example, Right, or #RIGHT, is the same as saying 8. So if you say Right=8 then whenever you use it in place of #RIGHT, it will work the same as in:
RIGHT=8
WHILE 1
  IF BUTTON() AND RIGHT THEN DEC X
WEND
So all buttons() can be handled like that. You can load those numbers to an array just as you would any other number If ever you need help, though I may not always be able to, go ahead and ask me. Lately I have had a spurt in my own learning and it is going somewhat quickly for now. I know a lot more now then I did literally three days ago, and likewise of last week

Cool, thanks! What does
button()
and
right
(or any variable I assume) mean as conditional statements?

Cool, thanks!
I take it that answered your question. You're welcome.

Cool, thanks! What does
button()
and
right
(or any variable I assume) mean as conditional statements?
Still, a confusing question. Right is a variable. To means the button specifically, unless you have assigned the variable to mean that, you must add "#" before it. Literally, it means if a button is pushed then something will happen. Saying if a button is pushed, and that button is, for example, #A, then you are saying a button is pushed as well as the number 16 currently true. Button() pretty much just means every button. With and, you are making the condition more specific.

So 8 (or right) being true means the condition "right" in
IF BUTTON() AND RIGHT
is satisfied?

And for 8 to be true the program also has to recognize we're looking for a button and that the button pressed (right) corresponds to the number 8?

There's some weird information in this thread. BUTTON() does return a bitfield representing the current button pressing state, and the constants #UP, #DOWN, #LEFT, #RIGHT, #A, #B, #X, #Y, #L, #R, #ZL, and #ZR provide named constants for comparing it ([http://smilebasic.com/en/reference/#constant]). You can check each button's bit with a bitwise AND comparison Usually an input loop looks like this
WHILE 1 'loop forever
  'poll the button state and store to variable B
  B = BUTTON()
  
  'check the first bit with bitwise AND
  IF B AND #UP THEN Y = Y + 1
  'the second bit 
  IF B AND #DOWN THEN Y = Y - 1
  '#UP and #DOWN could be replaced by "1" and "2" 
  'or &B1 and &B10 respectively
WEND
Hope this helps.

What does bitwise mean?

Cool, thanks! What does
button()
and
right
(or any variable I assume) mean as conditional statements?
You can just do it like this:
ACLS
WHILE 1
B=BUTTON()
IF B==16 OR B==#A THEN LOCATE 0,1 ?"BUTTON A IS BEING PRESSED" ELSE CLS
'16 is A

And for 8 to be true the program also has to recognize we're looking for a button and that the button pressed (right) corresponds to the number 8?
That's right. Just remember to specify that the variable RIGHT is equal to #RIGHT (8).