Incorrect work if and switch



  • There's a field of class object. PlayerisGround, everything checked, the meaning changes, but using the variable to ifin switchcase It's like he doesn't what a mistake is?

    void Move(Player& pl)
    {
        if (_kbhit())
        {
            char a = _getche();
            switch (a)
            {
            case 'a':
                pl.point_y = pl.point_y - 1;
                break;
            case 'd':
                pl.point_y = pl.point_y + 1;
                break;
            case 's':
                pl.point_x = pl.point_x + 1;
                break;
                if (pl.isGround == true)
                {
                case ' ':
                    pl.point_x = pl.point_x - pl.jumpforce;
                    cout << "JUMP" << endl;
                    break;
                }
            }
        }
        pl.ChangePositiont('$');
    }
    


  • The author &apos; s reply was of course entitled to life, but the code would be more correct and readable in the situation (on the recommendation of Harry in the comments)

    void Move(Player& pl)
    {
        if (_kbhit())
        {
            char a = _getche();
            switch (a)
            {
            case 'a':
                pl.point_y = pl.point_y - 1;
                break;
            case 'd':
                pl.point_y = pl.point_y + 1;
                break;
            case 's':
                pl.point_x = pl.point_x + 1;
                break;
            case ' ': 
                if (pl.isGround == true)
                {
                    pl.point_x = pl.point_x - pl.jumpforce;
                    cout << "JUMP" << endl;
                }
                break;
            }
        }
        pl.ChangePositiont('$');
    }
    


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2