Limiting KeyDown

BlitzPlus Forums/BlitzPlus Programming/Limiting KeyDown

jad(Posted 2007) [#1]
I use the KeyDown function to move a character across the screen. But when you press two keys at the same time eg. up and right, the character will move diagonally. How do you limit the character to moving along one axis at a time?


b32(Posted 2007) [#2]
I think like this:
rx = keydown(205) - keydown(203)
if rx <> 0 then
   x = x + rx
else
   y = y + keydown(200) - keydown(208)
end if

(edit:) wait, this one is better:



Moore(Posted 2007) [#3]
The below code limits movement to a single axis giving preference to the y axis. Arrange it with left and right first and you will give preference to the x axis.

; I make UP, DOWN, LEFT, RIGHT global variables that can be defined as any key. 
;This is great as it allows for user defined keyboard set ups.

If keydown(UP) Then
 ; add code here to move object accordingly
ElseIf keydown(DOWN) Then
 ; add code here to move object accordingly
ElseIf keydown(LEFT) Then
 ; add code here to move object accordingly
ElseIf keydown(RIGHT) Then
 ; add code here to move object accordingly
EndIf



jad(Posted 2007) [#4]
Thanks for the help. It now works.