C++ 中的 glutIdleFunc() 变量值未递增

Variable value not being incremented in glutIdleFunc() in C++

我试图在单击键盘上的字母 "b" 后不断增加变量 bulletY 直到最大 y 值 window 大小以产生射击效果。但是 bulletY 只递增一次。

这里是 bool flags 和 double 使用的初始值:

bool shoot = false;
bool isDone = false;
double bulletX = 8000 ;
double bulletY = -1;
double plusX = 0;
double plusY = 0;

下面是用于处理按键输入的方法

void key(unsigned char k, int x, int y)//keyboard function, takes 3 parameters
                                    // k is the key pressed from the keyboard
                                    // x and y are mouse postion when the key was pressed.
{
    if (k == 'b') { //SHOO
        shoot = true;
    }

    glutPostRedisplay();//redisplay to update the screen with the changes
}

最后,这是我创建的要传递给空闲函数的 Anim 函数 glutIdleFunc(Anim)

void Anim(){
if (shoot==true) {
        bulletX = plusX;
        bulletY = plusY;
        isDone = true;
    }

            if (isDone&& bulletY<450) {
                bulletY += 1;
                std::cout << "bullet Y is currently at : " << bulletY << "\n";
            }
            else {
                isDone = false;
                shoot = false;
            }
        glutPostRedisplay();
    }

好像在你的Anim()函数中bulletY总是重置为0,然后加1,所以bulletY一直都是1。

删除 bulletY = plusY(即 0),它应该在每次迭代中递增。