Advertisement

Simple Input Problem

Started by April 04, 2000 11:05 PM
3 comments, last by WymsicalWyvern 24 years, 8 months ago
Hi, I have a small problem. When controlling something say a spaceship, you press and hold the button controlling it to turn and go forward etc. so, its simple, just check to see if the button is pressed (btw i''m using direct input), The problem is here: I want the button ''q'' bring me back to the main menu, but then in the main menu, the button ''q'' should make me quit the program entirely. If i however use the same method as above, pressing ''q'' will quit the game instead of going to the main menu unless i was lucky enough to let go of the key quickly enough. How would i go by doing this, using only DirectInput?
I would do this the same way people seem to control buttons such as pause and stuff, to make sure they don''t react repeatedly to the same key press. When the key goes down, set a global flag that says the key has been pressed. Then when it first goes up, clear the flag. This means that when you check the key, you must check like this:

if(KEYDOWN(DIK_Q) && !qKeyPressed)
{
gameState = GAME_STATE_MENU;
qKeyPressed = !qKeyPressed;
}
else if(!KEYDOWN(DIK_Q) && qKeyPressed)
qKeyPressed = !qKeyPressed

I hope this is right, but I''m pretty sure it is. And I hope it helps.

------------------------------
Jonathan Little
invader@hushmail.com
http://www.crosswinds.net/~uselessknowledge
Advertisement
thx
If you dont mind the game pausing, use:

if (KEYDOWN(DIK_Q)){  while (KEYDOWN(DIK_Q);  gameState = GAME_STATE_MENU;} 


---------------
kieren_j
Heres a very good way to do it (I use it, and it works, in other words ). BTW, you need two seperate variables:
if(DIKEYDOWN(DIK_Q))
{
if(!Q_Pressed_Last_Cycle)
{
Q_Down = true;
}
else
{
Q_Down = false;
Q_Pressed_Last_Cylce = true;
}
}
else
{
Q_Pressed_Last_Cylce = false;
Q_Down = false;
}

Edited by - Zipster on 4/5/00 3:43:09 PM

This topic is closed to new replies.

Advertisement