繁体   English   中英

无法在 openGL 和 GLUT 中为我想要的确切对象设置动画

[英]Can't animate the exact object that I want in openGL & GLUT

当我在特定对象上单击右键时,它开始动画。 但是,无论我单击同一个对象来停止它 - 动画对象 - 还是任何其他没有动画的对象,动画对象都会停止。 但是,我希望只有当我点击它时才会停止动画对象。

我该如何解决这个问题? 这是我的 onClick 和 onTimer 函数。 我应该在结构中使用动画 bool 吗? 如果是这样,如何?

if(button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN) {

        for(i=0; i<MAX; i++){
            if(mx >= arr[i].xB + 0 && mx <= arr[i].xB + 130 && my <= arr[i].yB + 17 && my >= arr[i].yB + - 17){
                animation = !animation;
                animationNumber = i;
                printf("TRUE\n");
            }
        }

void onTimer( int v) {

    glutTimerFunc( TIMER_PERIOD, onTimer, 0) ;


    if(animation){
        arr[animationNumber].xB++;
    if(arr[animationNumber].xB >= 640)
        arr[animationNumber].xB -= 1410;
    }

    glutPostRedisplay() ; // display()

}

使用数组bool animation[MAX]而不是单个布尔变量animation和索引变量animationNumber
有了这个解决方案,每个对象都有自己的状态,表明它是“动画”还是静止不动。

bool animation[MAX] = {};

当鼠标按键被按下时,改变数组中对象的对应状态:

if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
{
    for (i=0; i<MAX; i++)
    {
        if (mx >= arr[i].xB + 0 && mx <= arr[i].xB + 130 &&
            my <= arr[i].yB + 17 && my >= arr[i].yB + - 17)
        {
            animation[i] = !animation[i];
            printf("TRUE\n");
        }
    }
}

遍历定时器函数中的所有对象并更新对象的位置:

void onTimer( int v) 
{
    glutTimerFunc( TIMER_PERIOD, onTimer, 0) ;

    for (i=0; i<MAX; i++)
    {
       if(animation[i])
       {
           arr[i].xB++;
           if( arr[i].xB >= 640)
               arr[i].xB -= 1410;
       }
    }
    glutPostRedisplay();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM