# stupid problem, if statement related i think

This topic is 5154 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have this stupid problem with my program, i was actually gonna put in the beginners forum but just incase it's not actually to do with the if statement and maybe some daft error i've made or the actuall functions i'm using. It's input code and uses w to move forward and s to move backward, however for some reason only the S button code runs, unless i uncomment the else statement then only the w code runs. here's the code
//walk forward
{
//stealth?
if(CEngine::m_InputReceiver->IsKeyDown(KEY_KEY_A) == true) //convert to shift
m_AnimState = STEALTH_F;
else
m_AnimState = WALKING_F;
}
else
{
m_AnimState = IDLE1;
}
//else
//walk backward
{
//stealth?
if(CEngine::m_InputReceiver->IsKeyDown(KEY_KEY_A) == true) //convert to shift
m_AnimState = STEALTH_B;
else
m_AnimState = WALKING_B;
}
else
{
m_AnimState = IDLE1;
}


maybe i just don't know how to use the input class i'm using, it's from the irrlicht website, i'm using that engine. any tips would be helpful.

##### Share on other sites
There are actually two different problems.

The first (with the "else") is simple. You don't want that "else". It makes no sense. Here is what you have:
if (bla == true) ... // <--- In case the key is pressedelse if (bla == false) ...// In case the key is not pressedelse ... // <--- In case the key is pressed AND not pressed. This will never happen.

The second problem (without the "else") is, that the S-button-handling overwrites the variable that the W-button handling has set. Your W-button handling works correctly and sets the variable, but since the S-button is not pressed the variable is set back to the Idle state.

Here's what you should do:
if (CEngine::m_InputReceiver->IsKeyDown(KEY_KEY_W)){	//stealth?	if (CEngine::m_InputReceiver->IsKeyDown(KEY_KEY_A)) //convert to shift		m_AnimState = STEALTH_F;	else		m_AnimState = WALKING_F;}else if (CEngine::m_InputReceiver->IsKeyDown(KEY_KEY_S)){	if (CEngine::m_InputReceiver->IsKeyDown(KEY_KEY_A)) //convert to shift		m_AnimState = STEALTH_B;	else		m_AnimState = WALKING_B;}else{	m_AnimState = IDLE1;}

(I removed the "== true" parts. They are not harmful, but unneeded and a bit confusing. But that may be a question of taste.)

##### Share on other sites
thanks very much for that, makes perfect sense now. i feel really stupid lol. i have been out of action on the programming side for ages and have just got back into it for the 4e4 contest. such obvious problems too.

And about the == parts, i hate them too its because I had a different function in there at first and forgot to take that out, i wasn't sure what the function was doing.

Anyway thanks again, now i can get back to coding.

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 16
• 11
• 9
• 25
• 54