Disable multiple keyhits

BlitzMax Forums/BlitzMax Beginners Area/Disable multiple keyhits

qim(Posted 2010) [#1]
My player moves smoothly on a grid, but I only want him to move horizontal or vertical. If I press KEY_UP and KEY_LEFT at once the sprite moves directly to upper left. I'd like to disable this. I don't quiet get it :-/

I tried something like this (it's pseudocode and surely won't work, but I hope you get my point):
Local MoveHorizontal:Int = false
Local MoveVertical:Int = false

If KeyDown(KEY_UP) And MoveHorizontal = False Then
  MoveUp()
  MoveVertical = True
End if

If KeyDown(KEY_LEFT) And MoveVertical = False Then
  MoveLeft()
  MoveHorizontal = True
End if

Function MoveUp()
  'Move Player Up
  MoveVertical = false
End Function

Function MoveLeft()
  'Move Player Left
  MoveHorizontal = false
End Function


But that doesn't work. :-(

Maybe I can disable multiple key hits, so I get only one key at once? Or is there an easier way?


GfK(Posted 2010) [#2]
Something like
If keydown(KEY_UP)
  'move up
ElseIf keydown(KEY_DOWN)
  'move down
ElseIf keydown(KEY_LEFT)
  'move left
ElseIf keydown(KEY_RIGHT)
  'move right
EndIf