Problem with Direct Input
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
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.
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.
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
Thanks
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:
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;}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement