Help with some code for reading keypresses
Moderator: Moderators
Just thinking ahead a little. I wondered about reading simultaneous button presses.tepples wrote:What kind of "combination" are you talking about?electro wrote:Wondering now how I might read combinations of buttons. Would I need to store another variable in ram called button_state2 ?
Thinking ahead to:
if a button and left arrow were pressed simultaneously then do something else.
I still have to try to get what I've learned so far working, but I think I am understanding it so far. Thanks to the help here.
Thanks again,
T
Well, you usually code a different action for each button, so if 2 buttons are pressed, both actions will be triggered. This is what allows you to jump while running in games. But of course, you can jump without running as well.
But if you want something to happen only when 2 (or more) buttons are pressed at the same time, I can think of 2 ways to do that. The easiest one would be check for all the buttons you want, one after the other, branching away if one of the required buttonsis not pressed:
Another way would be to directly AND the bits of the buttons you want to be pressed together, by shifting the number between the AND operations. Since the AND operation results in a "1" only when both bits are one, if any of the buttons is 0, the final result will be 0 and you'll know that they are not all pressed.
But if you want something to happen only when 2 (or more) buttons are pressed at the same time, I can think of 2 ways to do that. The easiest one would be check for all the buttons you want, one after the other, branching away if one of the required buttonsis not pressed:
Code: Select all
;Test first button
lda #%00010000
and ButonsPressed
beq Nocombo
;Test second button
lda #%10000000
and ButtonsPressed
beq NoCombo
;Whatever goes here is execute only if both buttons are pressed
NoCombo:
;If one or both buttons are not pressed, you end up hereA variable is just a piece of memory that holds information. That means that by simply storing something anywhere in RAM you are using that location as a variable.electro wrote:Could any point me to simple examples of setting up a variable in ram?
Code: Select all
lda #$50
sta $14The easiest way to declare a variable is to assign an address to a name somewhere in your code (usually at the top):
Code: Select all
MyVariable = $14
lda #$50
sta MyVariableAs your programs grow though, and you start using variables that occupy more than a single byte, this type of variable declaration becomes less appealing, because it becomes hard to maintain.
Most assemblers will allow you to reserve a number of bytes bytes from the current location on. So, if you start the declarations from a known location, you can just reserve the amount of bytes that each variable needs, like this:
Code: Select all
.org $0000
Variable1 .dsb 1
Variable2 .dsb 1
LargerVariable .dsb 3