为什么在第二次按下START时else if语句没有将loopCount设置为0

sy5wg1nm  于 2023-02-07  发布在  其他
关注(0)|答案(2)|浏览(83)

我试图设置loopCount = 1时,我按下开始一次,如果再次按下我想loopCount = 0。帮助将不胜感激。这里是代码

int frameCount = 0;
int loopCount = 0;
int buttonPressed = 0;
int backwardCount = 1;
void updateGame() {
    if (BUTTON_PRESSED(BUTTON_START)) {
        loopCount = 1;
    }
    else if (BUTTON_PRESSED(BUTTON_START) && loopCount == 1) {
        loopCount = 0;
    }
}
mf98qq94

mf98qq941#

第一个if将捕获BUTTON_PRESSED(BUTTON_START)true时的所有情况。由于您希望在按下按钮时切换loopCount,因此将两个if合并为一个,您只需切换变量:

if (BUTTON_PRESSED(BUTTON_START)) {
    loopCount = !loopCount;
}
2cmtqfgy

2cmtqfgy2#

将程序编写为

int frameCount = 0;
int loopCount = 0;
int buttonPressed = 0;
int backwardCount = 0;
void updateGame() {
    if(BUTTON_PRESSED(BUTTON_START)) {
        if(loopCount == 0) {
            loopCount = 1;
        } else if(loopCount == 1) {
            loopCount = 0;
        }
    }
}

相关问题