How do I use the same key twice in the same game?
BlitzPlus Forums/BlitzPlus Programming/How do I use the same key twice in the same game?
| ||
Hi! I've always made small games with blitzPlus but I recently started a bigger project so I have to use the same key(the keyHit function to be more specific) twice but only the first if statement gets executed. I've tried putting flushkeys() at the end of every if statement but it doesn't help. Thanks for any help |
| ||
indeed only the first keyhit seems to be taken into account : what i would do : (this is a blitz3d code...) |
| ||
Ugly untested code but you get the idea - test once per loop, store the value, compare against the stored value multiple times:Global KEY_LEFT:Int = whatever key code you want Global KEY_RIGHT:Int = whatever key code you want Global KEY_LEFT_PRESSED:Byte Global KEY_RIGHT_PRESSED:Byte Repeat Cls ParseKeys() If KEY_LEFT_PRESSED 'do something EndIf If KEY_RIGHT_PRESSED 'do something else EndIf 'Draw Stuff Flip Forever Function ParseKeys() KEY_LEFT_PRESSED = False KEY_RIGHT_PRESSED = False If KeyHit(KEY_LEFT) KEY_LEFT_PRESSED = True EndIf If KeyHit(KEY_RIGHT) KEY_RIGHT_PRESSED = False EndIf End Function |
| ||
Most people treat KeyHit() as True/False, meaning key was or was not hit. But what it really reports is the number of times the key was hit since the previous call to KeyHit().Graphics3D(640,480,32,2) ; Hit spacebar rapidly, observe keyhit values greater than 1. While Not KeyHit(1) Print KeyHit(57) Delay 500 Flip() Wend |