Skip to main content
gamedev.net
🔒 Locked

Problem with Direct Input

Started by mits Nov 15, 2004 at 1:51 AM 3 replies 1k views
Original Post
mits
mits
I have implemented a Direct Input class almost identical to this which is described in "Programming RPGs with DirectX". I have a question. My game consists of two states. I want to change from menu state to game state by pressing the LEFT arrow key. I also want to change from game state to menu state by pressing RIGHT arrow key. I use this: if(m_Input.KeyStateBuffer[DIK_LEFT]&&0x80) { ChangeState(GAMESTATE); } when I'm on menu state and if(m_Input.KeyStateBuffer[DIK_RIGHT]&&0x80) { ChangeState(MENUSTATE); } when I'm on game state. m_Input is my Direct Input class. The fact is that the states change continually without pressing any key when the program starts. I assume that the if statement is always true in both cases and that's why the states change by themselves. Any idea what might be wrong? Thanks
Endurion
Endurion
For checking a set flag you need to use &, not &&.

Using the if with && (logical and) will check both left and right side for true, and since 0x80 evaluates to always true you have permanent changing.
Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>
Muhammad Haggag
Muhammad Haggag
You're using '&&' (logical and) instead of '&' (bitwise and) [smile]

mits
mits
I tried it with "&". It happens the same thing. The only thing which change is the time space between state changes. But I still have many changes in one second. Any other idea?
Thanks
Endurion
Endurion
The check only checks if the key is down, not if it has been pressed once.

If you do this check every frame you will have a lot of changes. Add a bool to see if the key has been released again before allowing another change.

Something like this:

bool bLeftReleased = true;  // static or global or member of m_Input (advised!)if ( m_Input.KeyStateBuffer[DIK_LEFT] & 0x80 ){  if ( bLeftReleased )  {    bLeftReleased = false;    ChangeState(GAMESTATE);  }}else{  bLeftReleased = true;}
Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.