我有以下用作glutKeyboardFunc
函数参数的函数:
void handleKeypress(unsigned char key, //The key that was pressed
int x, int y) { //The current mouse coordinates
switch (key) {
case 27: //Escape key
exit(0); //Exit the program
}
if (key == 'a')
{
moveCircleLeft(0);
}
if (key == 'w')
{
moveCircleUp(0);
}
if (key == 's')
{
moveCircleDown(0);
}
if (key == 'd')
{
moveCircleRight(0);
}
}
现在我将向您展示如何编写函数moveCircleLeft
的示例:moveCircle
void moveCircleLeft(int x)
{
characterX = characterX - 0.1;
glutPostRedisplay();
x++;
if (x < 10)
{
glutTimerFunc(10, moveCircleLeft, x);
}
}
其他moveCircle
函数的工作方式相似,四个函数之间的唯一区别是它的正数是负 0.1 还是characterY
相反characterX
。
现在,问题来了:
所有方向都朝着正确的方向移动,但是当我尝试按键时,角色的移动会出现延迟/暂停。所以,如果我只按 d 键 (moveCircleRight),它会向右移动一点,停止 a 一小段时间,然后以恒定速度在屏幕上移动,没有停顿。然后,如果我换一个不同的键,它会在改变方向之前暂停一点,然后以恒定的速度朝那个方向移动。
任何有关此的建议将不胜感激!