Jump to content
  • Advertisement
Sign in to follow this  
JinJo

stupid problem, if statement related i think

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

If you intended to correct an error in the post then please contact us.

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
	if(CEngine::m_InputReceiver->IsKeyDown(KEY_KEY_W) == true)
	{
		//stealth?
		if(CEngine::m_InputReceiver->IsKeyDown(KEY_KEY_A) == true) //convert to shift
			m_AnimState = STEALTH_F;
		else
			m_AnimState = WALKING_F;
	}
	else
    if(CEngine::m_InputReceiver->IsKeyDown(KEY_KEY_W) == false)
	{
		m_AnimState = IDLE1;
	}
	//else
	//walk backward
	if(CEngine::m_InputReceiver->IsKeyDown(KEY_KEY_S) == true)
	{
		//stealth?
		if(CEngine::m_InputReceiver->IsKeyDown(KEY_KEY_A) == true) //convert to shift
			m_AnimState = STEALTH_B;
		else
			m_AnimState = WALKING_B;
	}
	else
	if(CEngine::m_InputReceiver->IsKeyDown(KEY_KEY_S) == false)
	{
		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 this post


Link to post
Share on other sites
Advertisement
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 pressed
else if (bla == false) ...// In case the key is not pressed
else ... // <--- 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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!