Key Input: 2 questions
Blitz3D Forums/Blitz3D Beginners Area/Key Input: 2 questions
| ||
1. How can I tell if a key is hit multiple times, or just once? For example if I want to check if key 57 has been hit 1 time 2 times or 3 times. once=punc twice=power punch three times= sliding punch? Do I check for keyhit or keydown multiple times? 2. I usually get keydown instead of keyhit. Even if I very quickly press the key down and release, is their a way to slow it down? Or should I use a delay when checking if the keyhit? |
| ||
Keyhit: This command returns the number of times a specified key has been hit since the last time you called the KeyHit() command. Check the ScanCodes for a complete listing of scancodes. The trick would be to set up your keyboard polling routine so that you know how long has passed between tests. e.g. only call the testing function once per second. Something like this... Graphics3D 800,600 cam = CreateCamera() cube = CreateCube() message$ = "use 'P' To punch" While Not KeyDown(1) timer = MilliSecs()-oldtimer If timer > 1000 oldtimer = MilliSecs() message$ = DetermineAction() EndIf PositionEntity cube, Rnd(20)-10, Rnd(20)-10, 20 RenderWorld Text 100,100,message$ Flip Wend Function DetermineAction$() Select KeyHit(25) Case 0 Return "use 'P' To punch" Case 1 Return "punch" Case 2 Return "superpunch" Case 3 Return "sliding punch" Default Return "haymaker" End Select Return "error" End Function |
| ||
BlackJumpers solution wouldn't be very useful though. Who wants a 1 second delay between player actions? A better system would store the last few button presses made (including absences of button presses) and then compare them against a database for combination matches. |
| ||
... it depends what you mean by 'action'. This modified version allows you to move around (cursor keys), but only polls the 'punch button' every second... ... obviously fine-tuning of the timer is possible - although there will always be an element of 'lag' with this approach as Wolron suggests. I think this is acceptable for a 'special attack' button, but probably not for the main interaction (such as movement in the code above.) Note that with this method it is possible to punch, kick and move all at the same time with no massively complicated 'hash table' of possible moves... If you have a huge number of possible attacks, Wolron's suggestion will require comprehensive design/coding. You will still have to decide on appropriate 'time-slices' with Wolron's approach in order to decide what 'not pressing a key' would look like !!! I'm not suggesting that this code is a good solution - just that in some situations it might have its merits. |
| ||
Thanks for the code guys! Everything seems to be working here, I appreciate it *waves* |