Sign in to follow this  
mits

Problem with Direct Input

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this