[英]Can't animate the exact object that I want in openGL & GLUT
When I clicked the right button on the particular object it gets started to animate.当我在特定对象上单击右键时,它开始动画。 However, whether I click on the same object to stop it -the animating one- or any other object which is not animating, the animating object stops.
但是,无论我单击同一个对象来停止它 - 动画对象 - 还是任何其他没有动画的对象,动画对象都会停止。 But, I want animating object to be stopped only when I click on it.
但是,我希望只有当我点击它时才会停止动画对象。
How can I fix this problem?我该如何解决这个问题? Here is my onClick and onTimer functions.
这是我的 onClick 和 onTimer 函数。 Should I use the animating bool in structure?
我应该在结构中使用动画 bool 吗? if so, how?
如果是这样,如何?
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()
}
Use an array bool animation[MAX]
instead of the single boolean variable animation
and the indexing variable animationNumber
.使用数组
bool animation[MAX]
而不是单个布尔变量animation
和索引变量animationNumber
。
With this solution each object has its own state which indicates if it is "animating" or stands still.有了这个解决方案,每个对象都有自己的状态,表明它是“动画”还是静止不动。
bool animation[MAX] = {};
Change the corresponding state of the object in the array, when the mouse button is pressed:当鼠标按键被按下时,改变数组中对象的对应状态:
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");
}
}
}
Traverse all the objects in the timer function and update the positions of the objects:遍历定时器函数中的所有对象并更新对象的位置:
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.