0
\$\begingroup\$

The game is a 2D platformer.

The problem I have is when I press the B button to crouch from standing, I do not know how to implement a function where B can be pressed again to move back to the standing sprite.

the issue is if i use the code presented below it will check every frame for B == Pressed, basically appearing as if the player never crouched at all. Could somebody help please?

if (facingRightSide == true)
        {
            if (currPad.Buttons.B == ButtonState.Pressed)
            {
                m_currState = AnimState.CrouchRight; 
                isCrouchedRight = true;                                
            }
        }

if (isCrouchedRight == true)
        {
            if (currPad.Buttons.B == ButtonState.Pressed)
            {
                m_currState = AnimState.FacingRight;
                isCrouchedRight = false;
            }
        }

Thank you for your time.

\$\endgroup\$
2
\$\begingroup\$

Try implementing a property to keep track of your previous pressed state.

So then you can do something like this instead:

ButtonState PreviousButtonState;

// Only action button press once
if (PreviousButtonState.Buttons.B != ButtonState.Pressed && currPad.Buttons.B == ButtonState.Pressed)
{
    if (m_currState == AnimState.FacingRight) {
        m_currState = AnimState.CrouchRight; 
    } else if (m_currState == AnimState.CrouchRight) {
        m_currState = AnimState.FacingRight; 
    }
}

//Store the previous button state for next iteration
PreviousButtonState = currPad;
\$\endgroup\$
  • \$\begingroup\$ Yes this is the best simplest way to go. Once you have it figured out and you have more states you are going in and out of and need lots of checking etc. it gets better to use the state pattern \$\endgroup\$ – lozzajp Apr 6 '17 at 11:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.