Incorrect work if and switch
-
There's a field of class object.
Player
♪isGround
, everything checked, the meaning changes, but using the variable toif
inswitch
♪case
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 ' 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('$'); }