在C ++中glutIdleFunc()中变量值未增加

时间:2018-10-11 21:39:35

标签: c++ if-statement opengl glut

我试图在键盘上单击字母“ b”后连续递增变量bulletY,直到达到最大y值窗口大小以产生射击效果。但是bulletY仅增加一次。

以下是bool标志的初始值,并使用了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();
    }

1 个答案:

答案 0 :(得分:2)

似乎bulletY在您的Anim()函数中总是重置为0,然后再递增1,所以bulletY一直都是1。

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

相关问题