Sign in to follow this  
JinJo

stupid problem, if statement related i think

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
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

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