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?
Limiting KeyDown
BlitzPlus Forums/BlitzPlus Programming/Limiting KeyDown I think like this:
(edit:) wait, this one is better:
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:
Graphics 800, 600, 0, 2 SetBuffer BackBuffer() x = 400 y = 300 Repeat ;white Color 255,255,255 Oval x - 5, y - 5, 11, 11 ;find out if a key is pressed for the first time If Not(down1 Or down2) Then down1 = KeyHit(203) Or KeyHit(205) down2 = KeyHit(200) Or KeyHit(208) End If ;read directions rx = KeyDown(205) - KeyDown(203) ry = KeyDown(200) - KeyDown(208) ;if movement stops, reset flags If rx = 0 Then down1 = 0 If ry = 0 Then down2 = 0 ;limit to one direction If down1 Then ry = 0 If down2 Then rx = 0 ;apply direction x = x + rx y = y + ry ;red Color 255,0,0 Oval x - 5, y - 5, 11, 11 Flip Until KeyHit(1) End
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
Thanks for the help. It now works.